Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 (Beadle) 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 perusing the Monastery: (2)
As of 2014-08-31 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls