Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Max of 23 and 27 is 23?

by ikegami (Patriarch)
on Jul 02, 2015 at 19:57 UTC ( [id://1133012]=note: print w/replies, xml ) Need Help??


in reply to Max of 23 and 27 is 23?

$#a returns a magical value in lvalue contexts. (It used to always returns a magical value, but this was optimized.)

$ perl -MDevel::Peek -e'@a=qw( a b c ); sub { Dump($_[0]) }->( $#a );' SV = PVMG(0x1d36630) at 0x1cfcfd0 REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x1cf6ab0 MG_VIRTUAL = &PL_vtbl_arylen MG_TYPE = PERL_MAGIC_arylen(#) MG_OBJ = 0x1d07d10

The behaviour you observe is a missing SvGETMAGIC(sv) to handle such scalars.

use strict; use warnings; use List::Util qw( max ); use Variable::Magic qw( cast wizard ); # Make $x a magical variable that always returns 5. cast(my($x), wizard( get => sub { ${ $_[0] } = 5 }, ) ); $x = 3; if ($ARGV[0]) { no warnings 'void'; 0+$x } print('max($x, 4)=', max($x, 4), "\n"); print('$x=', $x, "\n");
$ perl a.pl 0 max($x, 4)=4 $x=5 $ perl a.pl 1 max($x, 4)=5 $x=5

Replies are listed 'Best First'.
Re^2: Max of 23 and 27 is 23? (XS--)
by tye (Sage) on Jul 02, 2015 at 21:18 UTC
    The behaviour you observe is a missing SvGETMAGIC­(sv) to handle such scalars.

    Which is pretty much to be expected with the vast majority of XS code. Which is one reason why I almost never use List::Util routines. Must use XS for "speed!". Correctness, well, that doesn't matter quite as much.

    - tye        

      > Which is one reason why I almost never use List::Util routines. Must use XS for "speed!". Correctness, well, that doesn't matter quite as much. The authors care about correctness. However, they cannot fix bugs that they are not informed of.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found