Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
can you tell me, looking at the following code:
  1. what is wrong
  2. the most elegant way to fix it
  3. what can be done to avoid similar errors
UPDATE: this is bad code, I know. it's just a completely fake example I made up for this post. it's also deliberately contrived, just to make the real problem less apparent (and where's the fun, otherwise? :-). you have been warned. don't do this at home.
package SomeData; # UPDATE: the first version of "new" doesn't seem to # work on some platform/version combination (and it's # way too evil anyway). but since this is not the main # issue, I will use a more "sane" version :-) # sub new { bless \pop, shift } sub new { my $self = pop; bless \$self, shift } sub value { ${shift;} } sub add { my($self, $type, $value) = @_; if($type eq 'number') { return $$self += $value; } if($type eq 'string') { return $$self .= $value; } die "only string or number allowed"; } sub increment { my $self = shift; $self->add(number => 1); } package MyNumber; use base qw( SomeData ); sub add { my($self, $value) = @_; die "not a number" unless $value =~ /^\d+$/; $$self += $value; } sub subtract { my($self, $value) = @_; die "not a number" unless $value =~ /^\d+$/; $$self -= $value; } package main; my $answer = MyNumber->new(42); print $answer->value(), "\n"; $answer->subtract(1); print $answer->value(), "\n"; $answer->increment(); print $answer->value(), "\n";

King of Laziness, Wizard of Impatience, Lord of Hubris

In reply to inheritance turns back and bites by dada

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and nobody stirs...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2018-02-22 01:32 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (288 votes). Check out past polls.