Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Re: (jeffa) Re: A better mod (%) operator? by hossman
in thread A better mod (%) operator? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2024-04-19 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found