Mark Galeck
Mark Galeck

Reputation: 6385

strange error message in a complex, but legal Perl one-liner

>perl -e '$_ = q(t b[\)sizeof];); s/(t?(\w)(?:\s(\w))?\s(\w)(\[([^\]]+)\]))/eval $1/e'
Bareword found where operator expected at (eval 1) line 1, near ")sizeof"
    (Missing operator before sizeof?)

This is legal Perl, then why the error message? I have the latest Perl.

This is an SSCCE ; any one character less and the error message does not appear.

Upvotes: 0

Views: 137

Answers (1)

TLP
TLP

Reputation: 67900

The Perl code is valid, but you are trying to eval a string which is not valid Perl code. When I run this code and swap eval for print, it prints the string:

t b[)sizeof]

Now if I try and run this as Perl code I get:

> perl -we't b[)sizeof]'
Bareword found where operator expected at -e line 1, near ")sizeof"
        (Missing operator before sizeof?)
Unquoted string "sizeof" may clash with future reserved word at -e line 1.
syntax error at -e line 1, near "[)"
Execution of -e aborted due to compilation errors.

(You should always use warnings -w, even with one-liners)

This code does exactly what your evaluation is trying to do: It's trying to run that string as Perl code, and it fails because that string is not valid Perl code.

Also you should be careful when using eval, as it can do unexpected and catastrophical things to your computer. Usually, this kind of double evaluation is written using two of the /e modifiers, e.g.:

s/.../.../ee

Which is a bit more convenient than

s/.../eval .../e

Upvotes: 6

Related Questions