Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: inheritance turns back and bites

by tinita (Parson)
on Mar 01, 2004 at 13:41 UTC ( #332891=note: print w/ replies, xml ) Need Help??


in reply to inheritance turns back and bites

as others said already, in MyNumber, you have implemented the method add different from the one in SomeData. you have to decide which one you want.

also, in the new method of SomeData, you have a little error. if you do
sub new { bless \pop, shift }
it's like doing
sub new { bless \42, "MyNumber" }
and you can't bless a constant into an object. so do my $arg = pop; bless \$arg, shift


Comment on Re: inheritance turns back and bites
Select or Download Code
Re: Re: inheritance turns back and bites
by borisz (Canon) on Mar 01, 2004 at 13:51 UTC
    sub new { bless \pop, shift } it's like doing sub new { bless \42, "MyNumber" } and you can't bless a constant into an object. so do my $arg = pop; bless \$arg, shift
    I do not think this is wrong, since \pop is like \pop @_ and whatever I pop it is pushed and a pushed constant is a scalar.
    Boris
      sorry, but:
      perl -wle' sub new { bless \pop, shift } my $main = new ("ClassName", 42);' Modification of a read-only value attempted at -e line 2. This is perl, v5.6.1 built for i586-linux This is perl, v5.8.0 built for i686-linux

      am i overlooking something?

      Update: same result with
      This is perl, v5.8.2 built for i686-linux

        Interesting, your sample code did _not_ result in a errormessage for me on linux with perl 5.8.1! But it does with my old perl 5.6.1 on the same machine.
        Boris
        you are correct, but there should be something strange going on under the hood. it seems only Windows allows the bless \pop, shift trick.

        This is perl, v5.8.0 built for MSWin32-x86-multi-thread -> ok
        This is perl, v5.8.2 built for cygwin-thread-multi-64int -> ok
        This is perl, v5.8.0 built for i686-linux -> dies

        sorry for not checking this beforehand. however, the reason why it works on a platform and fails on another is beyond the scope of this thread :-)

        cheers,
        Aldo

        King of Laziness, Wizard of Impatience, Lord of Hubris

Re: Re: inheritance turns back and bites
by dada (Chaplain) on Mar 01, 2004 at 13:57 UTC
    it's like doing sub new { bless \42, "MyNumber" }
    no, it's like doing:
    sub new { bless $_[-1], $_[0] }
    which I can do. arguably a good idea, but I can. and in fact, that part works, at least in my perl interpreter. (note: the code is deliberately "strange" just to hide a bit the real problem :-).

    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-07-12 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls