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

Re: perl oop concept

by dsheroh (Parson)
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.


Comment on Re: perl oop concept
Select or Download Code
Re^2: perl oop concept
by mendeepak (Scribe) on Dec 05, 2012 at 06:51 UTC

    Thanks dsheroh. Got how the Script works.

    *=*=*dEEPAk*=*=*

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2015-07-01 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (17 votes), past polls