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

system ("echo \$?; echo \$PWD; echo \$?; echo \$HA; echo \$\?"); i run this in perl and it doesn't work however if i run this manually in the terminal window, echo $? would show 1. Note $HA has no value. how can i get this to work in perl?

Replies are listed 'Best First'.
Re: unix $? problem
by Velaki (Chaplain) on Sep 24, 2004 at 21:47 UTC

    When I run this manually in a terminal, I get the following output:

    $ echo $?; echo $PWD; echo $?; echo $HA; echo $? 0 /home/MyDir/projects/perl/monks 0 0
    When I run the perl code, I get:
    $ perl test_prog.pl 0 /home/MyDir/projects/perl/monks 0 0

    I find nothing weird in the code that you should return a 1, since the echo $HA as a shell command would only echo $HA or a blank line (if $HA is nothing), and thus be successful either way; hence, 0.

    Hope that helped,
    -v
    "Perl. There is no substitute."
Re: unix $? problem
by shenme (Priest) on Sep 24, 2004 at 21:44 UTC
    If I run this manually in a terminal window it always returns 0, just as it does using Perl.
    root@budgie ldap# echo $?; echo $PWD; echo $?; echo $HUZZAH; echo $?
    0
    /home/budgerie/temp/ldap
    0
    
    0
    
    This is on RedHat 7.2 Linux using Bash 2.05.8. What are you using? (Read this as you will have to tell us more to get a useful answer, which this isn't)
Re: unix $? problem
by jdalbec (Deacon) on Sep 25, 2004 at 03:54 UTC
    What shell are you using? I almost get what you describe from csh and tcsh but the shell stops interpreting commands once it gets an error. It never executes the final echo $?.
    0 /Users/john 0 csh: HA: Undefined variable.

    When Perl executes a one-argument system command and the string contains shell metacharacters like ;, it runs the command by passing it to sh -c. I don't see any documented way to change this.

    However, you could use
    system ("csh","-c","echo \$?; echo \$PWD; echo \$?; echo \$HA; echo \$ +\?");
    or whatever your shell is.