error from - eval "do script"

S

Sunil

Hi,
I do something like the following
eval "do \'$fullFileName\'"
because I know the script to be executed only at runtime. This works
fine for me. But I am not able to gracefully handle any syntax or other
errors in $fullFileName

Any pointers?

Thanks,
Sunil.
 
S

Sunil

Hi,
Look into the special variable $@.

I was already checking $@, Now I have changed

eval "do \'$fullFileName\'";
to
eval "do $fullFileName" ;


and it is working as I want.

Thanks,
Sunil
 
S

Sunil

I do something like the following
I was already checking $@, Now I have changed

eval "do \'$fullFileName\'";
to
eval "do $fullFileName" ;


and it is working as I want.

Looks like I spoke too soon

This works as I expect and I am able to handle errors using $@
eval do 'setvars.pl';

but my code like
my $fullFileName = 'setvars.pl';
my $evalStr = "do \'$fullFileName\'";
eval "$evalStr";
does not seem to set the $@ variable even though I am able to see the error
in the console.
 
B

Ben Morrow

Sunil said:
This works as I expect and I am able to handle errors using $@
eval do 'setvars.pl';

but my code like
my $fullFileName = 'setvars.pl';
my $evalStr = "do \'$fullFileName\'";
eval "$evalStr";
does not seem to set the $@ variable even though I am able to see the error
in the console.

Read perldoc -f do again. do() does not throw an exception (call
die()) under any circumstances, so you do not need an eval. Also, do
returns errors in either $! or $@, depending on whether it opened the
file or not. So you want something like:

my $fullFileName = 'setvars.pl';
$@ = undef;
do $fullFileName or die "do failed: " . ($@ || $!);

You should avoid string eval at all costs: it is very slow and can do
very unexpected things if you are not careful.

Ben
 
L

Lukas Mai

Ben Morrow said:
my $fullFileName = 'setvars.pl';
$@ = undef;

You don't have to clear $@. Perldoc -f eval says:
If there was no error, $@ is guaranteed to be a NULL string.
do $fullFileName or die "do failed: " . ($@ || $!);

HTH, Lukas
 
B

Ben Morrow

Lukas Mai said:
You don't have to clear $@. Perldoc -f eval says:
If there was no error, $@ is guaranteed to be a NULL string.

perldoc -f do doesn't though...
You are, however, correct: do does clear $@ anyway.
I guess it's implied by L<perlvar/$@>.

Ben
 
S

Sunil

This works as I expect and I am able to handle errors using $@
Read perldoc -f do again. do() does not throw an exception (call
die()) under any circumstances, so you do not need an eval. Also, do
returns errors in either $! or $@, depending on whether it opened the
file or not. So you want something like:

my $fullFileName = 'setvars.pl';
$@ = undef;
do $fullFileName or die "do failed: " . ($@ || $!);

You should avoid string eval at all costs: it is very slow and can do
very unexpected things if you are not careful.

Ben

Thanks Ben !!!
That Helped !
 
B

Brian McCauley

Ben Morrow said:
Read perldoc -f do again. do() does not throw an exception (call
die()) under any circumstances, so you do not need an eval. Also, do
returns errors in either $! or $@, depending on whether it opened the
file or not. So you want something like:

my $fullFileName = 'setvars.pl';
$@ = undef;
do $fullFileName or die "do failed: " . ($@ || $!);

IIRC $@ doesn't capture compilation errors on some older Perls.
Perhaps the OP is using one of those.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
M

Malcolm Dew-Jones

Sunil ([email protected]) wrote:

: but my code like
: my $fullFileName = 'setvars.pl';
: my $evalStr = "do \'$fullFileName\'";
: eval "$evalStr";
: does not seem to set the $@ variable even though I am able to see the error
: in the console.

perldoc -f eval

...
using
"eval" neither silences perl from printing warn-
ings to STDERR, nor does it stuff the text of
warning messages into "$@"
...

perhaps this is your situation.
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top