Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Why won't a hash in a hash work as a hash reference to create an object?

by moritz (Cardinal)
on Apr 07, 2012 at 21:12 UTC ( [id://963944]=note: print w/replies, xml ) Need Help??


in reply to Why won't a hash in a hash work as a hash reference to create an object?

sub new { my ($account) = @_;

That's the problem. The first argument that new gets is the name of the class. So instead write

sub new { my ($class, $account) = @_; return bless $accounts{$account}, $class; }

Replies are listed 'Best First'.
Re^2: Why won't a hash in a hash work as a hash reference to create an object?
by Lady_Aleena (Priest) on Apr 07, 2012 at 21:22 UTC

    Thank you moritz. Quick question, what is the value of $class supposed to be?

    Have a cookie and a very nice day!
    Lady Aleena

      It's the name of the class. If you call new as a method:

      my $obj = Class->new;

      ... then it'll be the invocant.

      (Don't not call new as a method. You set yourself up for all kinds of mess that way.)


      Improve your skills with Modern Perl: the free book.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-18 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found