Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Question about eval

by imp (Priest)
on Jan 16, 2007 at 07:16 UTC ( #594867=note: print w/ replies, xml ) Need Help??


in reply to Question about eval

require actually requires the file/module, so an error is raised when the file does not exist. You should wrap the require in an eval block (or expression). When using eval it is usually better to have the last statement within the eval always return true, and use that instead of $@ when testing for success. e.g. if (eval "something; 1;") {}

eval and require:

if(eval { require "foo.pm"; 1; } ) { print "loaded module ok\n"; }else{ print "could not load module $@\n"; }
eval and use:
if(eval " use foo; 1" ) { print "loaded module ok\n"; }else{ print "could not load module $@\n"; }
As for this line:
print petrol "hello there !";
The reason why that does not generate an error is that the first argument to print can be a bareword, which is interpreted as a filehandle name. e.g.:
print STDOUT "hello\n";
If you use strict and warnings (which you always should) then you will receive this error:
Unquoted string "petrol" may clash with future reserved word at p line + 3. Name "main::petrol" used only once: possible typo at p line 3. print() on unopened filehandle petrol at p line 3.


Comment on Re: Question about eval
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://594867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-12-20 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (98 votes), past polls