A most amusing annoyance
Came across this in my travels this morning:
$ perl -E 'sub x { say "y"}; my $x = 'x'; $x->();'
y
Undefined subroutine &main::1 called at -e line 1.
Why on earth does this compile and run in the first place? Fixing the shell quotes makes the problem go away.
Removing the ' from around 'x' also yields the same deparse. Adding spaces like ' x ' breaks with the syntax error you're probably expecting.
My guess is the shell doing something to the quoting based on the lack of spaces.
Using Perl one-liners from a Unix shell requires that you master two programming languages: Perl and your shell. Quoting is a basic feature of shell programming that you must understand if you want to avoid surprises with one-liners.
If your shell code sends corrupted perl source, don't expect perl to guess your mistake.
In most (all?) Unix shells, a parameter can be partly quoted. For example:
will print out "foobarbaz". It is this feature that allows parameters like
--foo="bar baz"
to work, so it's a valuable feature.Anyway, with your command, perl sees this:
And of course,
say
returns 1 if it is successful. So the behaviour you see is not really surprising.thank you that was helpful