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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.