Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Time command output is not stored into the variable.

by ungalnanban (Pilgrim)
on Apr 07, 2010 at 05:23 UTC ( [id://833188]=perlquestion: print w/replies, xml ) Need Help??

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



I want to store the time command output into a variable
I used the following code but It shows an ERROR. Please help me.

use strict;
use warnings;

#my $time =`time ls`;
my $time = `time`;
print $time;


Output Should be the following

real 0m0.000s
user 0m0.000s
sys 0m0.000s

--$ugum@r--
  • Comment on Time command output is not stored into the variable.

Replies are listed 'Best First'.
Re: Time command output is not stored into the variable.
by ikegami (Patriarch) on Apr 07, 2010 at 05:39 UTC
    And then there's the issue that the output you are expecting is from a sh builtin (not the external command from my first post), but you didn't ensure that sh is executed to interpret the command.
    my $time = `/bin/sh -c 'time ...'`;
Re: Time command output is not stored into the variable.
by ikegami (Patriarch) on Apr 07, 2010 at 05:30 UTC

    What error? A usage error like the following?

    Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose] [--portability] [--format=format] [--output=file] [--version] [--quiet] [--help] command [arg...]

    The program needs an argument, a command to time. This has nothing to do with Perl.

Re: Time command output is not stored into the variable.
by jwkrahn (Abbot) on Apr 07, 2010 at 06:18 UTC

    You probably want to use Perl's built-in times function.


      I know the time functions but I want to store the time command output in to a variable.
      The time command output is printed in STDERR.
      How can I store the output in to a variable, you have any idea?


      --$ugum@r--
        The output of time is printed to STDERR, but after all the redirections are performed. Therefore, to capture it, you have to start time in a subshell. This has nothing to do with perl, though.
        perl -e '$a=`(time ls) 2>&1 >/dev/null`;print "[$a]"'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found