Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > capture output from print command to variable in perl

Reply
Thread Tools

capture output from print command to variable in perl

 
 
championsleeper
Guest
Posts: n/a
 
      04-15-2005
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.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-15-2005
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
 
Reply With Quote
 
 
 
 
Chris Mattern
Guest
Posts: n/a
 
      04-15-2005
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?"
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-16-2005
championsleeper <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      04-16-2005

"championsleeper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>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.


 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      04-16-2005

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";

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-18-2005
(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
strepxe@yahoo.co.uk
Guest
Posts: n/a
 
      04-20-2005
thanks all - appreciate the very helpful answers for a newbie .....

 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      04-21-2005

Anno Siegel wrote:
> (E-Mail Removed) <(E-Mail Removed)> 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();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Best way to capture output of another command-line program in Win32? Peter A. Schott Python 4 02-07-2006 12:30 AM
Capture Windows command line output - put in text file Ernesto Python 1 02-01-2006 03:29 PM
Pull out only first field of ps command ( in Perl ) PERL RUN COMMAND tweetiebirds@gmail.com Perl Misc 2 03-26-2005 06:03 AM
how to capture large output in a perl variable vsmurthy Perl Misc 1 03-22-2005 05:24 PM



Advertisments