Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: OO Perl: calling a constructor within a class

by alien_life_form (Pilgrim)
on Sep 26, 2001 at 14:51 UTC ( #114780=note: print w/replies, xml ) Need Help??


in reply to OO Perl: calling a constructor within a class

Greetings.
I do this kind of things thusly:

package Foo; my %DEFAULTS= ( NAME=>'', USERNAME=>'', ); sub new { my($pkg,%rest)=(@_); if(ref($pkg)) { #called as $instance->new() #do something about this. ; } # Checks, maybe? and other stuff. # this overwrites the stuff in %DEFAULTS with the stuff in # in %rest, if applicable. bless { (%DEFAULTS,%rest) }, $pkg; } ## #Main ## my $foo=Foo->new(); # gets blank NAME, USERNAME my $bar=Foo->new(NAME=>'bar', USERNAME=>'baz'); print STDOUT "Your mileage WILL vary\n";
Cheers
alf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://114780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2021-05-16 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (152 votes). Check out past polls.

    Notices?