weird "perl -e" behaviour

F

Francois Wisard

Hi folks,

I encounter a strange "perl -e" behaviour since I'm running FreeBSD 5.1
(Perl 5.6.1). My one-liners are refused with strange error messages.

Some examples:

bash-2.05b$ perl -e "@a=('a'..'z');print $a[5]"
ARRAY(0x810115c)bash-2.05b$

or

bash-2.05b$ perl -e "$_="abc";s/b/c/g;print $_"
Undefined subroutine &main::ls called at -e line 1.

or even

bash-2.05b$ perl -e "@a=('a'..'z');print $#a"
Bareword found where operator expected at -e line 1, near "0a"
(Missing operator before a?)
syntax error at -e line 1, next token ???

Anyone knows what happens there? I'm tired of writing files when a
one-liner should have sufficed.

TIA

Francois


--
"The main reception foyer was almost empty but Ford
nevertheless weaved his way through it."

- Ford making his way out of Milliways whilst under the
influence of enough alchol to make a rhino sing.
 
F

Francois Wisard

bash-2.05b$ perl -e "$_="abc";s/b/c/g;print $_"
Undefined subroutine &main::ls called at -e line 1.

Should be "$_='abc';s/b/c/g;print $_" of course. Same error message

Francois
 
P

pkent

Francois Wisard said:
I encounter a strange "perl -e" behaviour since I'm running FreeBSD 5.1
(Perl 5.6.1). My one-liners are refused with strange error messages.

Someone's already pointed out that this is just bash interpolating
variables, e.g:
bash-2.05b$ perl -e "@a=('a'..'z');print $a[5]"
ARRAY(0x810115c)bash-2.05b$

I get the same. If we do this, to see what the argument really is:

$ echo "@a=('a'..'z');print $a[5]"
@a=('a'..'z');print [5]

And now it's obvious why we get the array ref.

bash-2.05b$ perl -e "$_="abc";s/b/c/g;print $_"
Undefined subroutine &main::ls called at -e line 1.

I get:

$ echo "$_="abc";s/b/c/g;print $_"
@a=('a'..'z');print [5]=abc;s/b/c/g;print @a=('a'..'z');print [5]

but then if I run ls -lab:

$ echo "$_="abc";s/b/c/g;print $_"
-lab=abc;s/b/c/g;print -lab

And so on... this is why I generally use single quotes for perl -e :-/

P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top