perl - Trouble with shift and dereference operator -
i have question regarding how left , right sides of ->
operator evaluated. consider following code:
#! /usr/bin/perl use strict; use warnings; use feature ':5.10'; $, = ': '; $" = ', '; $sub = sub { "@_" }; sub u { shift->(@_) } sub v { $s = shift; $s->(@_) } 'u', u($sub, 'foo', 'bar'); 'v', v($sub, 'foo', 'bar');
output:
u: code(0x324718), foo, bar v: foo, bar
i expect u , v behave identically don't. assumed perl evaluated things left right in these situations. code shift->another_method(@_)
, shift->another_method(shift, 'stuff', @_)
pretty common.
why break if first argument happens code reference? on undefined / undocumented territory here?
the operand evaluation order of ->()
undocumented. happens evaluate arguments before lhs (lines 3-4 , 5 respectively below).
>perl -mo=concise,u,-exec a.pl main::u: 1 <;> nextstate(main 51 a.pl:11) v:%,*,&,x*,x&,x$,$,469762048 2 <0> pushmark s 3 <#> gv[*_] s 4 <1> rv2av[t2] lkm/3 5 <0> shift s* 6 <1> entersub[t3] ks/targ,2 7 <1> leavesub[1 ref] k/refc,1 a.pl syntax ok
both using , modifying variable in same expression can dangerous. it's best avoid unless can explain following:
>perl -e"$i=5; $i,++$i,$i" 666
you use
$_[0]->(@_[1..$#_])
Comments
Post a Comment