Yokupoku Maioku
Yokupoku Maioku

Reputation: 503

Perl `chop` removes two characters instead of one

I'm using the command line to pass a string to a Perl program. For instance, this passes "stackOveflow" to the program hello

hello "stackOveflow"

I use chop to remove the last character from the last element of @ARGV

chop($ARGV[$#ARGV]);

and then print it

say "$ARGV[$#ARGV]";

I'm expecting "stackOveflow in the output, but instead I'm seeing stackOveflo

Can someone tell me why?

Upvotes: 0

Views: 229

Answers (1)

ikegami
ikegami

Reputation: 386416

hello "stackOveflow" tells the shell to execute the program hello and pass the string stackOveflow to it as an argument (similar to how my $s = "stackOveflow"; assigns the string stackOveflow to $x in Perl).

chop removes the last character of the string, so the value changes from stackOveflow to stackOveflo.

$ perl -E'say $ARGV[-1]; chop($ARGV[-1]); say $ARGV[-1]' "stackOveflow"
stackOveflow
stackOveflo

Upvotes: 1

Related Questions