The problem isn't with wantarry, it's something more low
level then that, observe a slightly modified version
that doesn't use wantarray...
use overload '*' => sub {
my $div = int(${$_[0]} / ${$_[1]});
my $mod = ${$_[0]} % ${$_[1]};
return ($div, $mod);
};
This still generates "30" for 90/30, regardless of
context ... it's as if overloaded operators are allways
called in some sort of bizarro scalar context where lists
are evaluated as their last element instead of their length...
package mod;
use strict;
use overload '*' => sub {
return ('yakko', 'wakko', 'dot');
};
sub new {
my ($class,$val) = @_;
return bless \$val, $class;
}
package main;
use strict;
my $i = mod->new(90);
my $j = mod->new(60);
my @x = $i * $j;
my $x = $i * $j;
print "@x\n";
print "$x\n";
__END__
dot
dot
Update: My bad, I totally spaced out on the fact that
lists in scalar context evaluate to the last element... i
was thinking of it just like an array.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|