Why is this in Perl:
@x=(0,2);
substr('abcd',@x)
evaluated as "cd"?
And this:
substr('abcd',0,2);
evaluated as "ab"?
The documented syntax of the substr
operator is
substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
not
substr EXPR,ARRAY
or the more generic
substr EXPR,LIST
This is reflected in the output of prototype
(although you can't always rely on this).
$ perl -E'say prototype "CORE::substr"'
$$;$$
substr
's 1st argument is evaluated in scalar context.substr
's 2nd argument is evaluated in scalar context.substr
's 3rd argument (optional) is evaluated in scalar context.substr
's 4th argument (optional) is evaluated in scalar context.@x
in scalar context is the number of elements it contains (2
in this case).
You can achieve what you want using the following:
sub mysubstr {
if (@_ == 2) { substr($_[0], $_[1]) }
elsif (@_ == 3) { substr($_[0], $_[1], $_[2]) }
elsif (@_ == 4) { substr($_[0], $_[1], $_[2], $_[3]) }
else { die }
}
my @x = (0, 2);
mysubstr('abcd',@x)
substr has a prototype as a built in function, so @x is not expanded is evaluated in a scalar context, which returns 2, so basically you are calling substr('abcd',scalar(@x))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With