Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: hash reference to itself

by VinsWorldcom (Prior)
on Aug 02, 2016 at 20:01 UTC ( [id://1169035]=note: print w/replies, xml ) Need Help??


in reply to Re^2: hash reference to itself
in thread hash reference to itself

You're right. Is this better?

#!perl use strict; use warnings; package MyPkg; sub new { my ($self, $name) = @_; my %hash = ( name => $name ); return bless \%hash; } sub print { my $self = shift; print "name: " . $self->{name}; } package main; my $obj = MyPkg->new('foo'); $obj->print();

Replies are listed 'Best First'.
Re^4: hash reference to itself
by Your Mother (Archbishop) on Aug 02, 2016 at 21:22 UTC

    Yeppers. Though I am not a fan of anything but a view layer (this would be main:: implicitly here) doing output so print embedded in methods/subs is, to me, code smell. I understand it's convenient for examples but it can rub off on newbies as a good practice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found