Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

how to get value of print command

by jaydot (Novice)
on May 13, 2010 at 06:52 UTC ( #839787=perlquestion: print w/replies, xml ) Need Help??
jaydot has asked for the wisdom of the Perl Monks concerning the following question:

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?.. .how do i change my code? help please.

Update: tnx..u solved the problem.. this is what i really need..tnx for ur time..

Replies are listed 'Best First'.
Re: how to get value of print command
by marto (Bishop) on May 13, 2010 at 06:59 UTC

    You're capturing the return value of the print command. If you simply want to assign a string to a variable:

    my $result = "hello world";

    Update: Please don't remove a question once you've posted it.

Re: how to get value of print command
by CountZero (Bishop) on May 13, 2010 at 07:39 UTC
    You have to do it the other way around: put the string to be printed into a variable and print the variable.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: how to get value of print command
by jau (Hermit) on May 13, 2010 at 08:29 UTC

    Maybe this is what you want:

    #!/usr/bin/perl use strict; use warnings; close STDOUT; open( STDOUT, '>', \( my $output ) ) or die "Can't open in-memory file: $!"; print "Hello world!\n"; # prints to $output print STDERR 'STDOUT: ' . $output;


    STDOUT: Hello world!
Re: how to get value of print command
by k_manimuthu (Monk) on May 13, 2010 at 15:33 UTC
    my $result = print "hello world\n";
    For the above code $result get return value of "1". You want to get $result is "Hello world" you will be use the sprintf function.
    my $var="Some Text"; my $result = sprintf "$var hello world "; print "$result\n";
      my $result = sprintf "$var hello world ";
      Don't do that. Not only is it a useless use of sprintf, but it is also an abuse of sprintf, whose 1st argument should be a format specification. This does the same thing:
      my $result = "$var hello world ";

        I tried it in different way to get the solution. Now i understand i should not use "sprintf", sorry for the inconvenience caused. I will give more attention for replying a thread in future.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://839787]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-03-18 08:49 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (229 votes). Check out past polls.