Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

taking an output in a variable

by s_gaurav1091 (Beadle)
on Aug 01, 2006 at 06:08 UTC ( [id://564926]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I am trying to store the output of a echo command into a variable but it's not getting stored into the variable.Can somebody please suggest something on this. Is it possible to store the output of a echo command into a variable.I am writting the code for refrence.Please help
my $res = `echo $JAVA_HOME`; print "rs is $res\n"; if($res){ print "exist\n"; } if(!$res){ print "doesn't exist\n"; }

Replies are listed 'Best First'.
Re: taking an output in a variable
by Corion (Patriarch) on Aug 01, 2006 at 06:21 UTC

    Please add use strict; to the top of your program. Then Perl will tell you what happens:

    Global symbol "$JAVA_HOME" requires explicit package name at 564926.pl + line 4.

    Perl sees the variable $JAVA_HOME and replaces it with its value, which is empty. The strict pragma prevents you from inventing variables.

    If all you want to do is get at the value of environment variables, these are all available in the %ENV hash:

    my $res = $ENV{JAVA_HOME};

    If you want to know how to pass strings that look like variable names to the shell via backticks, you need to quote the special chars:

    my $res = `echo \$JAVA_HOME`;
      If you want to know how to pass strings that look like variable names to the shell via backticks, you need to quote the special chars:
      my $res = `echo \$JAVA_HOME`;
      To be completely thorough and pedantic, you can also use the qx operator with single quotes:
      my $res = qx'echo $JAVA_HOME';

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: taking an output in a variable
by mickeyn (Priest) on Aug 01, 2006 at 06:16 UTC
    Hi there,

    Your mistake is that perl interpolates $JAVA_HOME before the echo command is executed and I guess $JAVA_HOME in your code isn't defined.

    Try $ENV{JAVA_HOME} to get the variable you want.

    If you insist (for any reason) to use the echo command's output or need to bypass the perl interpolation, write \$JAVA_HOME.

    Enjoy,
    Mickey

Re: taking an output in a variable
by ikegami (Patriarch) on Aug 01, 2006 at 06:17 UTC
    That should work fine. Did you check $& for an error? In any case, my $res = $ENV{JAVA_HOME}; should also do the trick (and do it much more efficiently).

    Update: mickeyn found the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://564926]
Approved by Crackers2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-25 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found