in reply to Re: What means: "bless { @_ } => $class;"?
in thread What means: "bless { @_ } => $class;"?

Better than I'd have answered. Just an addition, to be complete.

The return value of the sub is the last eval:ed statement, in this case the 'bless'. It returns the reference (new object).

It is a common idiom to place the bless last in 'new' methods, so the new object is returned.

(The one asking the question probably knows this, since it says so in the Camel book about 'bless'. But just to be safe.. the other part of the question is almost there in the Camel book, too.)

Update:
Surprising, but if merlyn says it is a pedagogical problem, I'll believe it. :-) (That would be Perl-only programmers? The "return last value by default" is quite Perl-specific, afaik?)

  • Comment on Re^2: What means: "bless { @_ } => $class;"?

Replies are listed 'Best First'.
Re^3: What means: "bless { @_ } => $class;"?
by merlyn (Sage) on Apr 24, 2006 at 13:12 UTC
    It is a common idiom to place the bless last in 'new' methods, so the new object is returned.
    The disadvantage of that is that you cannot call methods within the constructor to pick up class-related or instance-related initialization information.

    However, that's fairly rare for simple demonstrations. Hence, most people haven't seen the fact that more often in "real life", bless is done fairly early in the constructor, usually as soon as the hashref is constructed.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.