eval not working?

A

arif.rahim

HI,
I have two blocks of code that gives different result for eval.
The first block in online mode:
# perl
$tt='$wrong_value=~/NOT/';
$wrong_value="NOT me";
if ( eval $tt ) { print "RESULT >> That was not you \n"; }
------- Output ---------------------
RESULT >> That was not you

same code in "perl -e"

#perl -e '$tt='$wrong_value=~/NOT/'; $wrong_value="NOT me"; if ( eval
$tt ) { print "RESULT >> That was not you \n"; }'

-------Output-----------------------
[NO output]

Can you please explain what is happening here?

regards,
Arif
 
J

John W. Krahn

HI,
I have two blocks of code that gives different result for eval.
The first block in online mode:
# perl
$tt='$wrong_value=~/NOT/';
$wrong_value="NOT me";
if ( eval $tt ) { print "RESULT >> That was not you \n"; }
------- Output ---------------------
RESULT >> That was not you

same code in "perl -e"

#perl -e '$tt='$wrong_value=~/NOT/'; $wrong_value="NOT me"; if ( eval
$tt ) { print "RESULT >> That was not you \n"; }'

-------Output-----------------------
[NO output]

Can you please explain what is happening here?

You are using single quotes to delimit the program so you can't use them to
delimit the string inside the program.

What you need to do:

perl -e '$tt=q[$wrong_value=~/NOT/]; $wrong_value="NOT me"; if ( eval $tt ) {
print "RESULT >> That was not you \n"; }'



John
 
L

Lukas Mai

(e-mail address removed) schrob:
HI,
I have two blocks of code that gives different result for eval.
The first block in online mode:
# perl
$tt='$wrong_value=~/NOT/';
$wrong_value="NOT me";
if ( eval $tt ) { print "RESULT >> That was not you \n"; }
------- Output ---------------------
RESULT >> That was not you

same code in "perl -e"

#perl -e '$tt='$wrong_value=~/NOT/'; $wrong_value="NOT me"; if ( eval
$tt ) { print "RESULT >> That was not you \n"; }'

-------Output-----------------------
[NO output]

Can you please explain what is happening here?

Enable warnings:

$ perl -we '$tt='$wrong_value=~/NOT/'; $wrong_value="NOT me"; if ( eval $tt ) { print "RESULT >> That was not you \n"; }'
Useless use of numeric eq (==) in void context at -e line 1.
Name "main::wrong_value" used only once: possible typo at -e line 1.
Use of uninitialized value in pattern match (m//) at -e line 1.
Use of uninitialized value in numeric eq (==) at -e line 1.
Use of uninitialized value in eval "string" at -e line 1.
Use of uninitialized value in eval "string" at -e line 1.
Use of uninitialized value in eval "string" at -e line 1.

Print what you're executing:

$ echo perl -we '$tt='$wrong_value=~/NOT/'; $wrong_value="NOT me"; if ( eval $tt ) { print "RESULT >> That was not you \n"; }'
perl -we $tt==~/NOT/; $wrong_value="NOT me"; if ( eval $tt ) { print "RESULT >> That was not you \n"; }

This is what's happening (quoted parts marked with ^):

'$tt='$wrong_value=~/NOT/'; $wrong_value="NOT me"; if ( eval $tt ) { print "RESULT >> That was not you \n"; }'
^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

$wrong_value=~/NOT/ is not quoted, so the shell expands $wrong_value,
which is unset, so it is replaced by nothing.

HTH, Lukas
 
A

Arif

Thanks John,
it is a greate help. I did not know q[...] can be used this way...
well...
Regards,
Arif
 
T

Tad McClellan

Arif said:
I did not know q[...] can be used this way...


Then there are probably yet more ways of quoting that you
don't know about.

Find out about them by reading "Quote and Quote-like Operators" in:

perldoc perlop
 

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
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top