Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: hash reference to itself

by VinsWorldcom (Prior)
on Aug 02, 2016 at 17:11 UTC ( #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? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-12-02 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (22 votes). Check out past polls.

    Notices?