http://www.perlmonks.org?node_id=1007049


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.

    *=*=*dEEPAk*=*=*