All subroutines (or functions or methods or whatever you were taught to call them in CS101) in perl return a value. You can explicitely return a value by using a return statement, or, the last line is returned. For instance, in the function:

use strict; use warnings; sub foo { 1; } print foo, "\n";

You would see an output of 1. Now, you never explicitely returned a value of 1, but because it was the last thing on the line, Perl tried to do what you meant by returning 1. However, Perl is not psychic. So, when you see most constructors ending with:

bless $self, $class;

$self is not modified. An object is created when bless is evaluated. The following would do the same thing:

my $object = bless $self, $class; return $object;

Consult perldoc bless for more information. So, this:

bless ($self,$class); return $self;

should read:

return bless ($self, $class);

Also, note that the first element of @_ is the class name, not the second. So:

my ($self,$class)=@_;

Should really be the other way around, i.e.:

my ($class, $self) = @_; # and add a check that we're not copying # for good measure die ("This isn't a copy constructor") if (ref $class);

And unless you know what you're doing, you shouldn't use anything but a hashref for $self. I think you might want to google for @ISA and read up a little bit on inheritance.


Want to support the EFF and FSF by buying cool stuff? Click here.

In reply to Re: why is a 2-argument bless not working in my situation by Vautrin
in thread why is a 2-argument bless not working in my situation by eXile

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.