Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Question about __PACKAGE__

by gam3 (Curate)
on Jan 22, 2010 at 02:19 UTC ( #818871=note: print w/replies, xml ) Need Help??

in reply to Question about __PACKAGE__

These are exactly the same after the compile stage
__PACKAGE__->hello('text1'); Foo->hello('text1');
These are also the same
hello(__PACKAGE__, 'text1'); hello('Foo', 'text1');
The difference between f('x') and 'x'->f() is that if there is no sub f {} in Foo @INC will be used to look for one.

Look at UNIVERSAL and perltoot.

-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re^2: Question about __PACKAGE__
by sman (Beadle) on Jan 22, 2010 at 03:06 UTC
    Do you mean these four lines belonged to Foo are calling class method instead of object method? Therefore no object is created by them, right?
    __PACKAGE__->hello('test1'); Foo->hello('test2'); hello(__PACKAGE__, 'text3'); hello('Foo', 'text4');
    However, if I call this line:
    one object will be created. Am I right so far?
      Yes. Mind you, in practice, hello is probably only suppose to called one of the two ways.
      It is important to note that Perl OO does not have any "built in" concept of data. There is no default Constructor and the Destructor consists of a call to DESTROY.

      It might be more descriptive to rewrite Foo->new()->hello('test5'); as

      bless(\sub {}, 'Foo')->hello('test5');
      This just blesses a CODE_REF for fun. It could be a HASHREF or some other REF. The only real difference between
      bless(sub {}, 'Foo')->hello()
      Besides the fact that the blessed version can contain data, is that just before the blessed object is Garbage Collected, DESTROY is called with it as the argument ($x->DESTROY). UNIVERSAL contains a DESTROY method, so it always exists.
      -- gam3
      A picture is worth a thousand words, but takes 200K.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://818871]
[oiskuu]: (obvious question) do you "use threads;" before all of this?

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-07-26 15:23 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (397 votes). Check out past polls.