go ahead... be a heretic | |
PerlMonks |
Re: questions about bless's second argumentby GrandFather (Saint) |
on Nov 19, 2020 at 23:52 UTC ( [id://11123862]=note: print w/replies, xml ) | Need Help?? |
See perlobj for the full story. The short version is that the second argument is the 'class' of the instance being created. If you aren't using inheritance then it's not very interesting. However consider:
Prints: Me too: This stringPrintMeToo is derived from PrintMe and inherits the constructor 'new' from PrintMe. Calling the constructor (my $meToo = PrintMeToo->new('This string');) gets the inherited PrintMe constructor called but creates a PrintMeToo instance. Calling AsStr on the PrintMeToo instance gets the PrintMeToo version of AsStr, but that version calls up to the base class (PrintMe) to get the result from the base class AsStr to concatenate to the text provided by PrintMeToo's AsStr. Bottom line is, always use the two parameter version of bless and always use the first parameter to the constructor sub to provide the class name.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
In Section
Seekers of Perl Wisdom
|
|