Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Anonymous Hash in Constructor

by tobyink (Abbot)
on Oct 18, 2012 at 18:56 UTC ( #999796=note: print w/ replies, xml ) Need Help??


in reply to Anonymous Hash in Constructor

You can. It works either way.

use v5.10; use strict; use warnings; { package Local::Test; sub new1 { my $class = shift; bless {} => $class; } sub new2 { my $class = shift; my %hash = (); bless \%hash => $class; } sub does_it_work { say "yes, it works"; } } my $obj1 = Local::Test->new1; $obj1->does_it_work; my $obj2 = Local::Test->new2; $obj2->does_it_work;

And there's no rule that the blessed reference has to be a hashref; it can be any kind of reference. Hashrefs are usually the most convenient, but there are sometimes advantages to other structures. Arrayrefs tend to perform slightly better, though your object's internal structure is rarely the bottleneck in real life applications. If your object represents something file-like, then a blessed filehandle may be convenient. The URI package uses blessed references to scalar strings.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Anonymous Hash in Constructor
Download Code
Re^2: Anonymous Hash in Constructor
by Jim (Curate) on Oct 18, 2012 at 22:24 UTC

    I somehow find it instructive to do this…

    { package SomeClass; sub new1 { bless {} } sub new2 { bless \%hash } sub does_it_work { print "Yes, it works\n" } } $obj1 = SomeClass->new1; $obj1->does_it_work; $obj2 = SomeClass->new2; $obj2->does_it_work;

    …to your fine example. Removing all pragmas is intentional. This isn't quality code. It's simple code to make the rudiments of something clearer—to me, at least. I learned something by this reduction.

      Ah, but using strict and warnings demonstrates that nothing in the example is considered bizarro stuff that Perl will complain about.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        Ah, but using strict and warnings demonstrates that nothing in the example is considered bizarro stuff that Perl will complain about.

        Yes, indeed. Adding even more stuff to the example could introduce many new invaluable lessons. A complete, well-written Perl application could serve as a primer in good object-oriented programming. No doubt. But my point here was simply that I learned a little something by going a bit reduction-crazy. Noise reduction as pedagogical aid.

        Disregarding the original topic of this thread entirely, I learned more still by playing with this…

        { package Class; sub new { bless {} } # sub new { bless [] } # sub new { bless \$scalar } # sub new { bless \&sub } sub Method { print "This is a method happening.\n" } } $object = Class->new; $object->Method;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999796]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-07-30 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls