Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: perl oop concept

by dsheroh (Prior)
on Dec 04, 2012 at 10:16 UTC ( #1007049=note: print w/replies, xml ) Need Help??

in reply to perl oop concept

Try it and see.

As written, this won't compile under strict because $self doesn't exist outside of new, so I've removed that. The $self from within new is returned as its output and assigned to $obj, so I think where you wrote $obj->{$self}, you really just meant $obj.

With that minor adjustment and adding a call to Data::Dumper to show what's in $obj, the code becomes

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; package a; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } package main; my $obj = a->new; $obj->{avalue} = 10; print Dumper($obj);
and produces the output
$VAR1 = bless( { 'avalue' => 10 }, 'a' );
Of course, as already mentioned, it would be much better OO practice to create an accessor for avalue instead of working directly with the object's internals, but at least you now know a way to see whether your objects contain the data you expect them to.

Replies are listed 'Best First'.
Re^2: perl oop concept
by mendeepak (Scribe) on Dec 05, 2012 at 06:51 UTC

    Thanks dsheroh. Got how the Script works.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007049]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-05-26 22:05 GMT
Find Nodes?
    Voting Booth?