capture output from print command to variable in perl

C

championsleeper

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.
 
G

Gunnar Hjalmarsson

championsleeper said:
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;
 
C

Chris Mattern

championsleeper said:
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?"
 
T

Tad McClellan

championsleeper said:
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
 
T

Tintin

championsleeper said:
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.
 
N

nobull

championsleeper said:
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";
 
A

Anno Siegel

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
 
N

nobull

Anno said:
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();
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top