Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re: perl oop concept by dsheroh
in thread perl oop concept by mendeepak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (9)
    As of 2014-12-23 03:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (135 votes), past polls