Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re:^2 $self->{foo} in sub new {}

by flounder99 (Friar)
on Dec 22, 2003 at 14:50 UTC ( [id://316375]=note: print w/replies, xml ) Need Help??


in reply to Re: $self->{foo} in sub new {}
in thread $self->{foo} in sub new {}

It is usually a good idea to use the $class = ref($class) || $class so a new object can be created from an existing object.
use strict; use Data::Dumper; package MyClass; sub new { my ($class, $foo) = @_; print __PACKAGE__ . "->new called with $class, $foo\n"; $class = ref($class) || $class; print "\$class is now $class\n\n"; my $self = {foo => $foo}; return bless $self, $class; } package main; my $obj1 = MyClass->new('fooval1'); my $obj2 = $obj1->new('fooval2'); print Dumper($obj1, $obj2); __END__ Outputs: MyClass->new called with MyClass, fooval1 $class is now MyClass MyClass->new called with MyClass=HASH(0x15d54bc), fooval2 $class is now MyClass $VAR1 = bless( { 'foo' => 'fooval1' }, 'MyClass' ); $VAR2 = bless( { 'foo' => 'fooval2' }, 'MyClass' );

--

flounder

Replies are listed 'Best First'.
•Re: Re:^2 $self->{foo} in sub new {}
by merlyn (Sage) on Dec 22, 2003 at 15:07 UTC
      I stand corrected. I just had this hammered into my head from examples in the camel/panther/ram books (sorry I never bought the llama book, maybe I should have) and perltoot. I don't have the books in front of me so if one of these don't do it this way, my appologies. I'm just running on memory.

      --

      flounder

      ok, you managed to confuse me.
      i read the nodes you posted but i dont think i understood them completely, so could you or someone else give me a pointer where i lern to code nice and clean perl objects?

        merlyn's perlboot is pretty good.

        Yes, I know tchrist suckered a lot of people into thinking ref($proto) || $proto is OK.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Re:x3 $self->{foo} in sub new {}
by grinder (Bishop) on Dec 22, 2003 at 15:16 UTC
    It is usually a good idea to use the $class = ref($class) || $class so a new object can be created from an existing object.

    It is? Why would you want to do a thing like that?

    Yes, Perl lets you do this sort of thing, but unless you're doing some rather unusual factory/exemplar style OO programming it should be avoided. It just makes Perl's objects look more mysterious than they are.

    Sometimes I have wished to clone an object, but that is another issue entirely.

    Certainly if you have lots of objects of different packages being created dynamically and delegated to other objects and the package name is not be readily available, then sure, this is the way to go, but I suspect that this is not the case here. Think of it from the caller's point of view: are they able to say PackageName->new($foo) instead of $pn->new($foo)? Usually the answer is yes, so the ref($class) trick is not needed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found