capture output from print command to variable in perl

Discussion in 'Perl Misc' started by championsleeper, Apr 15, 2005.

  1. i know its possible to get the epoch time in perl using the command
    print time;
    how can i capture this in a variable in a perl script? i've got a
    feeling i'm missing something fundamental from my perl knowledge but
    can't find out how to do it in the books i've been through.
     
    championsleeper, Apr 15, 2005
    #1
    1. Advertising

  2. championsleeper wrote:
    > i know its possible to get the epoch time in perl using the command
    > print time;


    That statement uses two functions: print() and time(). time() *returns*
    the epoch time which is printed to STDOUT by print().

    perldoc -f time
    perldoc -f print

    > how can i capture this in a variable in a perl script?


    my $time = time;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 15, 2005
    #2
    1. Advertising

  3. championsleeper wrote:

    > i know its possible to get the epoch time in perl using the command
    > print time;


    "time" gets the epoch time. "print" just prints it.

    > how can i capture this in a variable in a perl script? i've got a
    > feeling i'm missing something fundamental from my perl knowledge but
    > can't find out how to do it in the books i've been through.


    time is a perl built-in function. Here you are then using the built-in
    function print to print the value it returns. You can also just assign
    that value to a variable:

    my $nowtime = time;

    perldoc -f time for more information on time. You should probably
    read perldoc -f print as well; you seem shaky on what print
    actually does.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Apr 15, 2005
    #3
  4. championsleeper <> wrote:

    > i know its possible to get the epoch time in perl using the command
    > print time;
    > how can i capture this in a variable in a perl script? i've got a
    > feeling i'm missing something fundamental from my perl knowledge but
    > can't find out how to do it in the books i've been through.



    See the "Assignment Operators" section in:

    perldoc perlop


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 16, 2005
    #4
  5. championsleeper

    Tintin Guest

    "championsleeper" <> wrote in message
    news:...
    >i know its possible to get the epoch time in perl using the command
    > print time;
    > how can i capture this in a variable in a perl script? i've got a
    > feeling i'm missing something fundamental from my perl knowledge but
    > can't find out how to do it in the books i've been through.


    In all your Perl scripting experience, have you never done something like:

    $var = 'value';

    or

    $var = 99;

    or

    $var = time;

    Amazing.
     
    Tintin, Apr 16, 2005
    #5
  6. championsleeper

    Guest

    championsleeper wrote:
    > i know its possible to get the epoch time in perl using the command
    > print time;
    > how can i capture this in a variable in a perl script? i've got a
    > feeling i'm missing something fundamental from my perl knowledge but
    > can't find out how to do it in the books i've been through.


    As others have pointed out you can simply put the return value of
    time() into a variable. But if you really had had a reason to want to
    capture the output of print to a variable.

    sub subroutine_i_cannot_alter {
    print time;
    }

    my $capture;
    {
    require AtExit;
    open my $capture_handle, '>', \$capture or die $!;
    my $saved_handle = select $capture_handle;
    my $restore_output_handle = AtExit->new(sub{ select $saved_handle
    });
    subroutine_i_cannot_alter();
    }

    print "Captured <<<$capture>>>\n";
     
    , Apr 16, 2005
    #6
  7. championsleeper

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    >
    > championsleeper wrote:
    > > i know its possible to get the epoch time in perl using the command
    > > print time;
    > > how can i capture this in a variable in a perl script? i've got a
    > > feeling i'm missing something fundamental from my perl knowledge but
    > > can't find out how to do it in the books i've been through.

    >
    > As others have pointed out you can simply put the return value of
    > time() into a variable. But if you really had had a reason to want to
    > capture the output of print to a variable.
    >
    > sub subroutine_i_cannot_alter {
    > print time;
    > }
    >
    > my $capture;
    > {
    > require AtExit;
    > open my $capture_handle, '>', \$capture or die $!;
    > my $saved_handle = select $capture_handle;
    > my $restore_output_handle = AtExit->new(sub{ select $saved_handle
    > });
    > subroutine_i_cannot_alter();
    > }
    >
    > print "Captured <<<$capture>>>\n";


    AtExit appears to be an interesting module, thanks for mentioning it.

    However, I don't see the advantage in using it here. It only hides
    the re-selection of the original output handle. This seems to do
    the same thing without the obfuscation:

    my $capture;
    {
    open my $capture_handle, '>', \$capture or die $!;
    my $saved_handle = select $capture_handle;
    subroutine_i_cannot_alter();
    select $saved_handle;
    }

    Anno
     
    Anno Siegel, Apr 18, 2005
    #7
  8. championsleeper

    Guest

    thanks all - appreciate the very helpful answers for a newbie .....
     
    , Apr 21, 2005
    #8
  9. championsleeper

    Guest

    Anno Siegel wrote:
    > <> wrote in comp.lang.perl.misc:
    > >
    > > {
    > > require AtExit;
    > > open my $capture_handle, '>', \$capture or die $!;
    > > my $saved_handle = select $capture_handle;
    > > my $restore_output_handle = AtExit->new(sub{ select

    $saved_handle
    > > });
    > > subroutine_i_cannot_alter();
    > > }

    >
    > AtExit appears to be an interesting module, thanks for mentioning it.


    Actually the generic AtExit is not a standard module but the less
    general purpose module, SelectSaver, is both more applicable here and
    now standard.

    > However, I don't see the advantage in using it here. It only hides
    > the re-selection of the original output handle.


    It also ensures that the handle is restored even if
    subroutine_i_cannot_alter() does not return but throws an exception.
    It's a bit like using local().

    {
    require SelectSaver;
    open my $capture_handle, '>', \$capture or die $!;
    my $restore_output_handle = SelectSaver->new($capture_handle);
    subroutine_i_cannot_alter();
    }
     
    , Apr 21, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ernesto
    Replies:
    1
    Views:
    784
    Ernesto
    Feb 1, 2006
  2. Peter A. Schott
    Replies:
    4
    Views:
    598
    Peter A. Schott
    Feb 7, 2006
  3. keto
    Replies:
    0
    Views:
    1,001
  4. Damjan Rems
    Replies:
    7
    Views:
    278
    Albert Schlef
    Apr 12, 2010
  5. vsmurthy
    Replies:
    1
    Views:
    159
Loading...

Share This Page