http://www.perlmonks.org?node_id=485441

aplonis has asked for the wisdom of the Perl Monks concerning the following question:

I can successfully eval a *.csh file in Perl using backticks line-by-line. But I would like a report if eval should fail. In the code below, $@ gives complaints as if the contents of the backticks were Perl code. So even if the eval and backticks both run fine, $@ has needless complaints. Is there a workaround?
my $os_says = ''; my $pl_says = ''; if ( open FH, '/home/foo/bar.csh' ) { while (<FH>) { # no warnings; eval { $os_says .= `$_` }; # <- Fixed. Thanks Roger! if ($@) { $pl_says .= $@ } } print "OS says: $os_says \n"; print "Perl says: $pl_says \n"; }

Roger fixed this. I had been using parens, not braces, with my eval.

Replies are listed 'Best First'.
Re: $@ with backticks inside eval
by fizbin (Chaplain) on Aug 21, 2005 at 01:33 UTC

    First off, what a bizarre problem to be solving. Are you certain you're solving the correct problem?

    Secondly, what's happening is that you're sending the contents of $os_says to perl's eval. The code you have currently is equivalent to:

    $os_says .= `$_`; eval($os_says);
    What you want to do right now (as was already pointed out) is switch to:
    eval { $os_says .= `$_` };

    However, once you do that, you'll have the case that $@ will always be the null string, no matter if the backquoted command failed or not.(*) If you want to check on the return status of the executed command, check $!.

    But let me repeat - I do not think you are doing what you want to do. First off, you're executing every line with /bin/sh, and not with /bin/csh, even if csh is your default shell. Secondly, each line is getting executed in its own subprocess, so even if you were executing the lines with csh, this two line combination would produce an "Undefined variable" error:

    setenv ADP_INPUT_HOME /disk4/vendor/adp/ echo "Looking for files in $ADP_INPUT_HOME"

    Now, it would be possible to open a csh shell in the background and feed it one line at a time, and I suppose you could monitor the csh shell to see if it exited, but that's a much, much more advanced problem than you seem to be aiming for here.

    Why on earth are you trying to do this?

    (*) Okay, so I think $@ might contain something if the system were so overloaded that a subprocess couldn't be forked to do the backquote magic.
    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      Thanks for the detailed write-up. You are absolutely right! I had started out piping in a *.pl file line-by-line. Then I decided to extend that to other scripts and got in a rut.

      I have gone back and re-written that sub to backtick the script as a whole: *.pl, *.csh or whatever. That works a whole lot better. Thanks!

Re: $@ with backticks inside eval
by Roger (Parson) on Aug 20, 2005 at 23:42 UTC
    eval { ... }.
      That was it exactly. Thanks!
Re: $@ with backticks inside eval
by Roy Johnson (Monsignor) on Aug 21, 2005 at 00:46 UTC
    Why are you using eval at all?

    Caution: Contents may have been coded under pressure.

      The example was cobbled together hastily for this post and otherwise irrevelent.

      My actual use is inside an XML-RPC server script on a remote PC. With eval, if the backticked script fails my server won't die.

      Actual use detailed here