Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

put system(); output into my $variable -- chech for installed aplications

by baxy77bax (Deacon)
on Jan 15, 2009 at 12:47 UTC ( #736530=perlquestion: print w/replies, xml ) Need Help??

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

hi

i'm looking for a way to put result of a system('which perl') into my pre-defined variable.. but only for one system function , i do not want this to be the case for the rest of the system calls.

something like

my $x; $x = system("which perl"); print "-- $x --"; #this will print: /usr/bin/perl # -- 0 --
the reason why i'm asking this question is, i want to check if some of the applications are installed on my os (windows/linux), but they probably are not installed in the right place, but rather just added into the $PATH additionally. so this looked like the quickest and most portable way to do this. any alternatives are welcomed. <> p thanks
  • Comment on put system(); output into my $variable -- chech for installed aplications
  • Download Code

Replies are listed 'Best First'.
Re: put system(); output into my $variable -- chech for installed aplications
by prasadbabu (Prior) on Jan 15, 2009 at 12:57 UTC
    You have to use backtictks (``) or qx// to get the result and store the same in a variable. See about them in perlop and perlfunc.

    Prasad

Re: put system(); output into my $variable -- chech for installed aplications
by Bloodnok (Vicar) on Jan 15, 2009 at 12:57 UTC
    System doesn't return anything to the caller other than the return code and signal number. Use backticks, or qx//, instead e.g. my $var = `command`; or my $var = qx/command/;

    Compare system with Regexp-Quote-Like-Operators

    A user level that continues to overstate my experience :-))
Re: put system(); output into my $variable -- chech for installed aplications
by Corion (Pope) on Jan 15, 2009 at 12:59 UTC

    See system for why your code returns 0.

    The which program does not exist on Windows, so your idea won't work anyway. But the easiest way to get the output of a subprocess is to call it using backticks `...` or qx(). See perlop on both.

    If you want to find programs on the path, you will need to use File::Spec to split up the path into its parts and then check whether a file (possibly amended by [doc://Config]::Config::_exe) exists in that location, and is executable. See, again, perlop about the -X operators.

Re: put system(); output into my $variable -- chech for installed aplications
by 13warrior (Acolyte) on Jan 15, 2009 at 17:17 UTC
    Use open or backticks instead of system . System doesn't capture the output of the command.
    Open : open ($filehandle ,"/usr/bin/which prg|") or die "Error $!\n"; while(<$filehandle>) { ... } Backticks : my $output = `/usr/bin/which prg`; chomp $output;
Re: put system(); output into my $variable -- chech for installed aplications
by repellent (Priest) on Jan 15, 2009 at 18:22 UTC
    Others have mentioned how to capture the output using backticks/open.

    To get the name used to execute the current copy of Perl, make use of the variable $^X (that's dollar_sign - caret - uppercase_x). See perlvar under $EXECUTABLE_NAME.
    print "$^X\n"; # this will print realpath to perl
Re: put system(); output into my $variable -- chech for installed aplications
by bingos (Vicar) on Jan 15, 2009 at 20:44 UTC

    The best thing for portability would be to use File::Which

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2020-07-05 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?