Overloading

Discussion in 'Perl Misc' started by Ben Morrow, Dec 6, 2003.

  1. Ben Morrow

    Ben Morrow Guest

    I've been playing with overloading, and the following is puzzling
    me... the script

    #!/usr/bin/perl -l

    use warnings;
    use strict;

    {{

    package I;

    use overload
    "0+" => sub {
    my $s = shift;
    my $v = $$s;
    print "numify";
    0+$v;
    },
    q{""} => sub {
    my $s = shift;
    my $v = $$s;
    print "stringify";
    "$v";
    },
    "=" => sub {
    my $s = shift;
    my $v = $$s;
    print "copy";
    my $t = $v;
    bless \$t;
    },
    "-" => sub {
    my $s = shift;
    my $v = $$s;
    my $a = shift;
    print "sub($a)";
    $$s = (shift) ? $v - $a : $a - $v;
    };

    sub new {
    my $c = shift;
    my $s = shift;
    bless \$s, $c;
    }

    }}

    my $i = new I "*";
    print $i;
    print int($i);
    print $i--;

    __END__

    prints

    stringify
    *
    numify
    Argument "*" isn't numeric in addition (+) at ./op line 15.
    numify
    Argument "*" isn't numeric in addition (+) at ./op line 15.
    0
    sub(1)
    Argument "*" isn't numeric in subtraction (-) at ./op line 35.
    stringify
    1

    about which I have two questions:

    1. Why is 'numify' called twice for int($i)?

    2. Why is 'numify' not called at all before calling '-'? OK, I guess
    that makes sense... but why, then, will '-' not autogenerate in
    terms of 'numify'? Is there any way to call it, without giving the
    sub a name (i.e., is it possible to simply 'numify' a scalar, as
    "$s" will stringify)?

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    |----------------+---------------| The Levellers, 'Believers'
     
    Ben Morrow, Dec 6, 2003
    #1
    1. Advertising

  2. Ben Morrow

    Jim Keenan Guest

    "Ben Morrow" <> wrote in message
    news:bqsm47$149$...
    > I've been playing with overloading, and the following is puzzling
    > me... the script
    > [snip]
    >
    > prints
    >
    > stringify
    > *
    > numify
    > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > numify
    > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > 0
    > sub(1)
    > Argument "*" isn't numeric in subtraction (-) at ./op line 35.
    > stringify
    > 1
    >
    > about which I have two questions:
    >
    > 1. Why is 'numify' called twice for int($i)?
    >
    > 2. Why is 'numify' not called at all before calling '-'? OK, I guess
    > that makes sense... but why, then, will '-' not autogenerate in
    > terms of 'numify'? Is there any way to call it, without giving the
    > sub a name (i.e., is it possible to simply 'numify' a scalar, as
    > "$s" will stringify)?
    >


    In attempting to reproduce your problem, I rearranged the code so as to put
    package I at the top of the file, then explicitly called package main. I
    also threw in some newlines for readability. The result: package main ran
    without warnings.

    package I;
    use overload
    "0+" => sub {
    my $s = shift;
    my $v = $$s;
    print "numify";
    0+$v;
    },

    q{""} => sub {
    my $s = shift;
    my $v = $$s;
    print "stringify";
    "$v";
    },

    "=" => sub {
    my $s = shift;
    my $v = $$s;
    print "copy";
    my $t = $v;
    bless \$t;
    },

    "-" => sub {
    my $s = shift;
    my $v = $$s;
    my $a = shift;
    print "sub($a)";
    $$s = (shift) ? $v - $a : $a - $v;
    };

    sub new {
    my $c = shift;
    my $s = shift;
    bless \$s, $c;
    }

    1;

    package main;
    use warnings;
    use strict;

    my $i = new I "*";
    print $i, "\n";
    print int($i), "\n";
    print $i--, "\n";

    __END__
    # above code prints:

    stringify*
    numify0
    sub(1)stringify1
     
    Jim Keenan, Dec 7, 2003
    #2
    1. Advertising

  3. Ben Morrow

    Ben Morrow Guest

    "Jim Keenan" <> wrote:
    > In attempting to reproduce your problem, I rearranged the code so as to put
    > package I at the top of the file,


    Why?

    > then explicitly called package main.


    package is lexical, so the braces make sure we go back to main::.

    > I also threw in some newlines for readability.


    ....which is why I used '#!/usr/bin/perl -l', which puts in all those
    newlines and several more.

    > The result: package main ran without warnings.


    ....because you didn't turn them on early enough. Well done.

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    |----------------+---------------| The Levellers, 'Believers'
     
    Ben Morrow, Dec 7, 2003
    #3
  4. Ben Morrow

    Jim Keenan Guest

    "Ben Morrow" <> wrote in message
    news:bqvoj3$3ih$...
    > "Jim Keenan" <> wrote:
    > > In attempting to reproduce your problem, I rearranged the code so as to

    put
    > > package I at the top of the file,

    >
    > Why?
    >

    I find it makes the code more readable in cases where I'm including >1
    package in a file rather than pulling one in via 'use'. I developed this
    practice while working thru code examples in Damian Conway's "Object
    Oriented Perl."

    > > then explicitly called package main.

    >
    > package is lexical, so the braces make sure we go back to main::.
    >
    > > I also threw in some newlines for readability.

    >
    > ...which is why I used '#!/usr/bin/perl -l', which puts in all those
    > newlines and several more.
    >

    Okay.

    > > The result: package main ran without warnings.

    >
    > ...because you didn't turn them on early enough.


    Okay ... so then I went back to your original posting and copied-and-pasted
    your code exactly as you typed it. When I ran it, I was unable to reproduce
    the first of the two problems you cited:

    > 1. Why is 'numify' called twice for int($i)?


    > numify
    > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > numify
    > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > 0


    I did not get the first 2 lines -- only the last 3.



    and Well done.
    >
    > Ben
    >
    > --
    > If you put all the prophets, | You'd have so much more reason
    > Mystics and saints | Than ever was born
    > In one room together, | Out of all of the conflicts of time.
    > |----------------+---------------| The Levellers,

    'Believers'
     
    Jim Keenan, Dec 7, 2003
    #4
  5. Ben Morrow

    Ben Morrow Guest

    "Jim Keenan" <> wrote:
    > Okay ... so then I went back to your original posting and copied-and-pasted
    > your code exactly as you typed it. When I ran it, I was unable to reproduce
    > the first of the two problems you cited:


    They're not exactly 'problems', more things I am curious to know the
    reason for..

    > > 1. Why is 'numify' called twice for int($i)?

    >
    > > numify
    > > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > > numify
    > > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > > 0

    >
    > I did not get the first 2 lines -- only the last 3.


    Interesting. Which version of perl is this? I'm using 5.8.2.

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
     
    Ben Morrow, Dec 8, 2003
    #5
  6. Ben Morrow <> wrote in message news:<br0fmv$k5l$>...
    > "Jim Keenan" <> wrote:
    >
    > > > 1. Why is 'numify' called twice for int($i)?

    >
    > > > numify
    > > > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > > > numify
    > > > Argument "*" isn't numeric in addition (+) at ./op line 15.
    > > > 0

    > >
    > > I did not get the first 2 lines -- only the last 3.

    >
    > Interesting. Which version of perl is this? I'm using 5.8.2.
    >

    Ah, the plot thickens! The version on which I first tested both your
    original script and my re-ordering it was 5.6.1. Today I'm working
    with 5.8.0; testing *both* scripts reproduces your original finding:
    the doubling of the 'numify' printout and the subsequent warning.
    Implication: This was something that changed between 5.6.1 and 5.8.0.

    Perhaps we should turn this over to p5p!

    Jim Keenan
     
    James E Keenan, Dec 8, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,414
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    451
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    434
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    581
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    404
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page