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

Re: hash reference to itself

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


in reply to hash reference to itself

Perl can do object-oriented code.

#!perl use strict; use warnings; package MyPkg; sub name { return 'foo'; } sub print { print "name: " . name(); } package main; print MyPkg->name() . "\n"; MyPkg->print();

UPDATE: Actually *not* OO as YourMother points out below.

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

    There is no object here, just class calls which might as well be package calls.

    print MyPkg::name(), $/; MyPkg::print();

      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();

        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.

Re^2: hash reference to itself
by ExReg (Priest) on Aug 02, 2016 at 17:30 UTC

    I think if I had the time to do it, OO may have been a better way to do it. But for now, I will have to stick with the huge hashes I have. Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found