Click to See Complete Forum and Search --> : perl: my first package


YaRness
01-24-2001, 10:32 AM
here's all i have right now. i dunno if the "use strict" is necessary/redundant in the Yar.pm, but i figure stick with what i know until i know more.

Yar.pm

#!/usr/local/bin perl -w
#
package Yar;
#
sub new
{
my $self = {};
bless($self); # but see below
return $self;
}
#
sub foo
{
print STDOUT "foo\n";
}
#
return 1;



test.pl

#!/usr/local/bin/perl -w
use strict;
use Yar;
my $yarobj = new Yar;
$yarobj -> foo;

and it does exactly what it looks like it does, prints "foo\n".

i'm working on this cuz, one of the scripts i just wrote, i basically copied the whole thing, modified a couple bits, and used it for something else... so time to learn to modulate it more. and i could use some practice using objects.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
01-24-2001, 12:21 PM
ok, now i can handle optional hash-like parameters in the "new" function.

Yar.pm

#!/usr/local/bin perl -w
use strict;

package Yar;

sub new
{
my $type = shift;
my $self = {};
my %params = @_;
foreach my $label (%params)
{
$self->{$label} = $params{$label};
}
bless($self); # but see below
return $self;
}


sub foo
{
print STDOUT "foo\n";
}

return 1;



package.pl (instead of test.pl)

#!/usr/local/bin/perl -w
use strict;
use Yar;
my $yarobj = new Yar (name=>"YaRness",age=>"23");
print $yarobj ->{'name'},$yarobj ->{'age'},"\n";

now i'm gonna try and figure out how to check if the parameters given are what we want. so if a Yar object can only take name and age, if we do "my $yarobj = new Yar (foo=>"bar");" it'll generate an error.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
01-24-2001, 03:24 PM
ok, now it handles new(param=>"foo") calls, and even speaks up if you put in the wrong type of parameter.

Yar.pm

#!/usr/local/bin perl -w
package Yar;
use strict;
use Carp qw(cluck);
#

sub new
{
my $class = shift;
my $self = {};
my %params = @_;
foreach my $token (keys %params)
{
if (exists &$token)
{
$self->{uc $token} = $params{$token};
}
else
{
cluck "Bad token";
}

}
bless($self);
return $self;
}

sub name
{
my $self = shift;
if (@_) {$self->{'NAME'} = shift;return 1}
return $self->{'NAME'};
}

sub age
{
my $self = shift;
if (@_) {$self->{'AGE'} = shift;return 1}
return $self->{'AGE'};
}

return 1;


package.pl

#!/usr/local/bin/perl -w
use strict;
use Yar;
my $yarobj = new Yar(name=>"YaR",age=>"23",foo=>"bar");
print "Output: ",$yarobj->name,$yarobj->age,"\n";



<edit> changed some stuff in Yar.pm, including "use Carp". the cluck function gives a more detailed location of where the error occurred (it's like "warn").
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

[This message has been edited by YaRness (edited 24 January 2001).]

[This message has been edited by YaRness (edited 24 January 2001).]

TheLinuxDuck
01-24-2001, 03:38 PM
YaR:

I may not be posting in here, but I'm following this thread.. I know almost nothing about packages, and still don't quite get how it works.

I'll have to copy your examples and play with em to see what kind of damage I can do..

actually, I spent much of yesterday and today reading up on security/tainting issues with CGI's.. pretty qool stuff.. also, I've been reading about subroutine attributes.. you can set up a sub to receive a value, as:

mySub()=10;

And it will take the value that way, if the sub was defined as:

sub mySub:lvalue

Weird.. hoo-duh-tun-kit?

It looks like you're making good progress on the package stuff.. I'm gonna keep my eye on this thread, pardner! http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq