http://www.perlmonks.org?node_id=1019849


in reply to Perl semi-object without a constructor

Given this

package Msg; sub connect { my ($pkg, $to_host, $to_port, $rcvd_notification_proc) = @_; } # ... Msg->connect($host, $port, \&rcvd_msg_from_server);

The connect method is not being called with three arguments, it's being called with four arguments. The first argument is the string "Msg" - that is, the part before the ->. This is how method calls work in Perl (we borrowed it from Python); the thing before the arrow gets passed in as the first argument.

"From the comment, an object was created. The reference is the anonymous hash with 2 members, and the class name is what? $pkg appears to be an undefined scalar?"

$pkg is "Msg".

"From the prior 'bless', a $conn has no methods"

Why do you think $conn has no methods? Something to do with the hash only having a couple of uninteresting key-value pairs? An object's methods have nothing to do with the blessed reference itself, but are taken from the package it is blessed into.

So because $conn is blessed into the Msg package, all the subs defined within Msg are available as methods for $conn.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Perl semi-object without a constructor
by Wiggins (Hermit) on Feb 21, 2013 at 13:51 UTC
    "Why do you think $conn has no methods? "
    Because of my ignorance of the nuances of Perl classes and objects. All of the texts I looked at started with a package and a sub called 'new' to create the object. I had been looking at Msg.pm as a way to carve out and localize a set of functions and have a cleaner organization. That is the only usage I knew about for packages.

    This exploration has been enlightening, and gives me a new light to look at Perl packages. The simple explanation of:

    The first argument is the string "Msg" - that is, the part before the ->. This is how method calls work in Perl (we borrowed it from Python); the thing before the arrow gets passed in as the first argument.
    Is the key that I never found in my frantic scanning of my library; I didn't find an explanation of the mechanics, just the conceptualization of OO.

    Thanks a bunch! odd, ending my text with an exclamation point messes up formatting.

    !-- Node text goes above. Div tags should contain sig only -->

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.

      "All of the texts I looked at started with a package and a sub called 'new' to create the object."

      It is convention to name constructors new but there's no requirement to do so. You could just as well call it old or frangipane and it would make not one whit of difference to the Perl interpreter. But the reason that you should call a constructor new (and not old or frangipane) is for humans reading your code. When they see new they'll know precisely what that method call is doing!

      That said, it's quite common for a class to define additional constructors such as new_from_json or new_from_yaml in addition to their standard new constructor. These are often wrappers around new that do additional pre-processing or post-processing.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name