Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: perl oo

by OeufMayo (Curate)
on Jan 15, 2001 at 23:36 UTC ( [id://52019]=note: print w/replies, xml ) Need Help??


in reply to perl oo

From my poor knowledge on OO Perl, the first parameter of your call to 'new' is the name of the package or class.

If you write it this way:

sub new { my $class = @_; my $self = ( user => $_[1], session => 4556, ); bless($self, $class); return $self; }

and it should do what you're looking for.

<kbd>--
PerlMonger::Paris(http => 'paris.pm.org');</kbd>

Replies are listed 'Best First'.
(Ovid - quick fix to your new constructor) Re(2): perl oo
by Ovid (Cardinal) on Jan 15, 2001 at 23:48 UTC
    OeufMayo: you have the right idea, but you're on the wrong track and are a bit confused about context.
    my $class = @_;
    The left side is a scalar, which means the array '@_' is being called in scalar context. As a result, $class will be set to the number of elements in @_.

    Also, you want curly braces '{}' in the "my $self =" line to create the reference to the anonymous hash. The following should work:

    sub new { my ( $class ) = @_; my $self = { user => $_[1], session => 4556, }; bless($self, $class); return $self; }
    Putting the parenthesis around $class in the first line of the sub causes it to be evaluated in list context and grabs the first element of @_. There are still better ways to write that, but I'll leave it at that for now.

    Hope this helps!

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found