Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: OO Perl: calling a constructor within a class

by alien_life_form (Pilgrim)
on Sep 26, 2001 at 14:51 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://114780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found