error from - eval "do script"

Discussion in 'Perl Misc' started by Sunil, Oct 27, 2003.

  1. Sunil

    Sunil Guest

    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.
     
    Sunil, Oct 27, 2003
    #1
    1. Advertising

  2. Sunil

    Sunil Guest

    > > 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?

    >
    >
    > 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
     
    Sunil, Oct 27, 2003
    #2
    1. Advertising

  3. Sunil

    Sunil Guest

    Re: error from - eval "do script" - unsolved: does not set $@

    > > > 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?

    > >
    > >
    > > 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.


    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.
     
    Sunil, Oct 27, 2003
    #3
  4. Sunil

    Ben Morrow Guest

    Re: error from - eval "do script" - unsolved: does not set $@

    "Sunil" <> wrote:
    > 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

    --
    . | .
    \ / The clueometer is reading zero.
    . .
    __ <-----@ __
     
    Ben Morrow, Oct 27, 2003
    #4
  5. Sunil

    Lukas Mai Guest

    Re: error from - eval "do script" - unsolved: does not set $@

    Ben Morrow <> wrote:

    > 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
     
    Lukas Mai, Oct 27, 2003
    #5
  6. Sunil

    Ben Morrow Guest

    Re: error from - eval "do script" - unsolved: does not set $@

    Lukas Mai <> wrote:
    > Ben Morrow <> wrote:
    >
    > > 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.


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

    Ben

    --
    don't get my sympathy hanging out the 15th floor. you've changed the locks 3
    times, he still comes reeling though the door, and soon he'll get to you, teach
    you how to get to purest hell. you do it to yourself and that's what really
    hurts is you do it to yourself just you, you and noone else *
     
    Ben Morrow, Oct 27, 2003
    #6
  7. Sunil

    Sunil Guest

    Re: error from - eval "do script" - unsolved: does not set $@

    > > 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


    Thanks Ben !!!
    That Helped !
     
    Sunil, Oct 28, 2003
    #7
  8. Re: error from - eval "do script" - unsolved: does not set $@

    Ben Morrow <> writes:

    > "Sunil" <> wrote:
    > > 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: " . ($@ || $!);


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

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Oct 28, 2003
    #8
  9. Re: error from - eval "do script" - unsolved: does not set $@

    Sunil () 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.
     
    Malcolm Dew-Jones, Oct 28, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,616
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    581
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,554
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    156
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    176
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page