http://qs321.pair.com?node_id=1169017


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.