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

Re: getting the output of command in variable

by rkrieger (Friar)
on Feb 01, 2007 at 14:13 UTC ( #597746=note: print w/replies, xml ) Need Help??


in reply to getting the output of command in variable

You may want to review your use of system() and check the manual (perlfunc) for that command. It tells you that system() is not for capturing the output of the command performed.

You're probably looking for backticks or the qx{} operator. These (usually) interpolate, so you can use variables in the command invocation. See perlop for more information. It also contains pointers on redirecting the STDOUT and STDERR for your command.

The code below printed "2007-02-01 15:14:19 CET" on my console.

#!/usr/bin/perl use strict; use warnings; my $date_cmd = '/bin/date'; my $date_format = q{ "+%Y-%m-%d %H:%M:%S %Z" }; # See date(1), str +ftime(3) chomp(my $date = qx{ ${date_cmd} ${date_format} } ); print "${date}\n";

Update: Pretty-printed code; added reference to output redirection (perlop manual); fixed tense in text.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://597746]
help
Chatterbox?
[Discipulus]: is not the problem with new hardware?
[marto]: notepad++ ticks all the boxes for me, it's a fine line between just enough, and bloat, in editors for me :P
[marto]: Discipulus]: less so than the modern intel CPUs
[marto]: also, AMD are faster at fixing the CPU microcode it would seem
[Discipulus]: anyway i never bought a PC in my life: few videocards and some disks: i recycle old things

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2017-07-27 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (407 votes). Check out past polls.