hi just wanna ask is it possible for perl to get the value of a print command? for ex.

my $result = print "hello world\n";
print "$result\n";

why is it that the "print $result" would print a value which is 1?.. is it possible to get the result "hello world" not 1?... how do i do it?.. help please..

hi just wanna ask is it possible for perl to get the value of a print command? for ex.

my $result = print "hello world\n";
print "$result\n";

why is it that the "print $result" would print a value which is 1?.. is it possible to get the result "hello world" not 1?... how do i do it?.. help please..

That's weird. I can't explain why print $result would print a value which is 1. The value of $result should be zero (which prints as a blank when unformatted) if the print "hello world\n"; succeeded, and I don't know why it would not.

As for saving whatever you printed: to do that I think you need to redirect your STDOUT to a file.

Yesterday I was using the perl debugger to test and for some reason it printed the value of $result as blank, indicating zero. But when I run the following script (not using the interactive debugger) the value of $result is 1.

#!/usr/bin/perl
use strict;
use warnings;
my $result = print "hello world\n", 42, "\nhello again world\n", "goodbye world\n";
print $result . "\n";

#Output is:
#hello world
#42
#hello again world
#goodbye world
#1

I am not sure what you mean, mitchems, when you say the result is a count of the output.

Edited 6 Years Ago by d5e5: n/a

I messed that up actually. I did a bit of additional researcha nd realized that it is returning 1 as in "true", meaning there is valid output from the command. I was thinking it was counting the result, but that's not true. I tried a couple of other things like ($result)=print "hello world\n" and that kind of thing. Itis always set to 1, so I suppose it is to indicate that the print command is true. Make sense?

I messed that up actually. I did a bit of additional researcha nd realized that it is returning 1 as in "true", meaning there is valid output from the command. I was thinking it was counting the result, but that's not true. I tried a couple of other things like ($result)=print "hello world\n" and that kind of thing. Itis always set to 1, so I suppose it is to indicate that the print command is true. Make sense?

Yes. That must be it. A return of 1 (meaning true) means the print command was successful.

Hi, I have been thinking about this problem for a while and thought of a way that it would work by redirecting a local version of STDOUT.

my $result;
{
	local *STDOUT;
	open(STDOUT, ">",\$result) or die $!;
	print "Hello World!\n";
	close STDOUT;
}
print "result is $result";

Neat. I didn't know you could print to a scalar variable. But then I haven't read all the documentation:icon_redface:

You can if you use a typeglob (*STDOUT) and a reference to the scalar. I was reading "Advanced Perl Programming" on the train this morning and noticed that the author redirected (aliased) STDERR to a file handle. So I started messing around with it in various contexts and found that you have to localize the print command and redirect STDOUT to the reference. If you don't, you can't actually print out the result. Once the STDOUT redirection has passed out of scope the result goes into the result variable, because it is in a global scope. By closing STDOUT (which is a "file handle", but since file handles can't be treated as scalars, you have to typeglob it), STDOUT goes back to the way it was before the aliasing and you can print the result.

Pretty dang cool, if I do say so myself. In reality it's just using references, with which I was familiar, yet it took some testing to show it can work with STDOUT.

Comments
Good explanation
This article has been dead for over six months. Start a new discussion instead.