in reply to Re: nested variable resolution
in thread nested variable resolution

It worked, thanks! If you have a moment, can you tell me what the difference between using {} and "" is? I'll look it up in perlfunc as well, but if you have any insight. Thanks a ton! Matt

Replies are listed 'Best First'.
Re^3: nested variable resolution
by Argel (Prior) on Jul 17, 2007 at 22:49 UTC
    The 'eval EXPR' version evaluates code at runtime like you expect while the 'eval BLOCK' version is used for exception handling (and is not evaluated at runtime).

    For example normally DBI errors are fatal to the entire program. However, if I wrap my DBI calls in an eval BLOCK structure I can then catch a fatal DBI error and take steps to deal with that in my program such as logging it, restarting the database connection, etc. The important thing is that the fatal error doesn't take my script with it.

    Personally I think using the same name is confusing. I would have preferred something like 'catch BLOCK' instead (or perhaps 'exception BLOCK').

    Here's some 'eval BLOCK' code I use:

    sub cmd_wrapper { my( $cmd ) = @_; my( $rc , $out ); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 15; $out = (`$cmd`)[0]; # Get first line of output $rc = $?; alarm 0; }; if( $@ ) { die "Eval failed for $cmd but not because of alarm" if $@ ne " +alarm\n"; # Propogate unexpected errors die "Eval failed for $cmd because alarm timed out"; } die "Return code undefined for $cmd" unless defined $rc; return $rc, $out if wantarray; return $rc; }

    See also Corion's post: Re: nested variable resolution.

Re^3: nested variable resolution
by FunkyMonk (Bishop) on Jul 17, 2007 at 22:02 UTC
    eval explains it much better than I can. It's your first port of call if you're having problems with eval.

    Have a look, and if you're still having problems, ask!