Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

Comment on how to get value of print command
Download Code
Re: how to get value of print command
by marto (Chancellor) 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.

    CountZero

    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;

    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://839787]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-10-02 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (63 votes), past polls