Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:

use strict; use warnings; package PrintMe; sub AsStr { my ($self) = @_; return $self->{str}; }; sub new { my ($class, $str) = @_; return bless {str => $str}, $class; } package PrintMeToo; use base 'PrintMe'; sub AsStr { my ($self) = @_; return 'Me too: ' . $self->SUPER::AsStr(); }; package main; my $meToo = PrintMeToo->new('This string'); print $meToo->AsStr();

Prints:

Me too: This string

PrintMeToo 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 reply to Re: questions about bless's second argument by GrandFather
in thread questions about bless's second argument by Special_K

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found