in reply to Re^2: eval problem
in thread eval problem
Your evaluating --$1, where -- is the predecrement operator.
Seems pretty straight forward to meI read the docs on eval(), and I have no idea what it does
[For eval EXPR], the return value of EXPR is parsed and executed as if it were a little Perl program. The value of the expression (which is itself determined within scalar context) is first parsed, and if there weren't any errors, executed in the lexical context of the current Perl program.
As for errors,
If there is a syntax error or runtime error, or a die statement is executed, eval returns an undefined value in scalar context or an empty list in list context, and $@ is set to the error message. If there was no error, $@ is guaranteed to be a null string.
I couldn't get a single example using eval to work in perl.
The difference is that you can build the code dynamically$ perl -le'print "Hello, World!";' Hello, World! $ perl -le'eval q{print "Hello, World!";};' Hello, World!
$ perl -le'print "3$ARGV[0]4";' + 3+4 $ perl -le'print eval "3$ARGV[0]4"' + 7 $ perl -le'print eval "3$ARGV[0]4"' '*' 12
Be careful about using external inputs:
$ perl -le'print $ARGV[0]' '`rm -i *`' `rm -i *` $ perl -le'print eval $ARGV[0]' '`rm -i *`' rm: remove regular file `demo.pl'? ^C
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: eval problem
by 7stud (Deacon) on Dec 04, 2009 at 01:25 UTC | |
by ikegami (Patriarch) on Dec 04, 2009 at 02:18 UTC | |
by ikegami (Patriarch) on Dec 04, 2009 at 04:57 UTC |