Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: substr odd behavior with list arguments

by choroba (Archbishop)
on Mar 22, 2021 at 16:13 UTC ( #11130136=note: print w/replies, xml ) Need Help??


in reply to substr odd behavior with list arguments

That's because prototype(\&CORE::substr) is $$;$$. It means each argument is evaluated in scalar context, so in the second case @a is interpreted as scalar @a, i.e. the size of the array, which is 2.

Use

my ($offset, $length) = a_subroutine_call_that_returns_offset_and_leng +th(@some_args); substr('abcdefg', $offset, $length);

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: substr odd behavior with list arguments
by wfischer (Novice) on Mar 22, 2021 at 16:26 UTC
    That's the "why" -- thanks for that clear explanation. (And, yes, that's the workaround I use.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (None)
    As of 2021-05-10 03:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Perl 7 will be out ...





      Results (104 votes). Check out past polls.

      Notices?