Ben Morrow
Guest
Posts: n/a

 12-06-2003
me... the script

#!/usr/bin/perl -l

use warnings;
use strict;

{{

package I;

"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.
http://www.velocityreviews.com/forums/(E-Mail Removed) |----------------+---------------| The Levellers, 'Believers'

Jim Keenan
Guest
Posts: n/a

 12-07-2003

"Ben Morrow" <(E-Mail Removed)> wrote in message
news:bqsm47\$149\$(E-Mail Removed)...
> 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;
"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

Ben Morrow
Guest
Posts: n/a

 12-07-2003
"Jim Keenan" <(E-Mail Removed)> 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.
(E-Mail Removed) |----------------+---------------| The Levellers, 'Believers'

Jim Keenan
Guest
Posts: n/a

 12-07-2003
"Ben Morrow" <(E-Mail Removed)> wrote in message
news:bqvoj3\$3ih\$(E-Mail Removed)...
> "Jim Keenan" <(E-Mail Removed)> 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.
> (E-Mail Removed) |----------------+---------------| The Levellers,

'Believers'

Ben Morrow
Guest
Posts: n/a

 12-08-2003

"Jim Keenan" <(E-Mail Removed)> 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] (E-Mail Removed)

James E Keenan
Guest
Posts: n/a

 12-08-2003
Ben Morrow <(E-Mail Removed)> wrote in message news:<br0fmv\$k5l\$(E-Mail Removed)>...
> "Jim Keenan" <(E-Mail Removed)> 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