How can I print to a variable in Perl?

Yasir

I have already defined a bunch of functions which do a lot of work and have a bunch of print statements. They can be called like so to build an html page.

print_A()
print_B()
print_C()

Now, I want to call these functions and store the contents of these print statements into one main variable. One way is to rewrite these functions so they return a string with their contents (instead of printing)

my $var = "";
$var = $var . store_A();
$var = $var . store_B();
$var = $var . store_C();

But I want to do this without modifying or rewriting the functions. I don't want to redefine these functions with such a minor change (there are hundreds of these functions in the program).

Is there a shorter and faster way to do this in perl?

toolic

One way is to use select to redirect STDOUT to a scalar variable:

use warnings;
use strict;

my $out;
open my $fh, '>', \$out;
my $old_stdout = select $fh;
s1();
s2();
select $old_stdout;
close $fh;

print "start\n";
print $out;
print "end\n";

sub s1 {print "s1\n"}
sub s2 {print "s2\n"}

Prints out:

start
s1
s2
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I print a multidimensional array in Perl?

From Dev

How can I print the address of a bool variable?

From Dev

How can I print a variable to a file?

From Dev

How can I print a variable to stdout in Postscript

From Dev

How can i print the value of a variable to a Textview

From Dev

How can I make Perl print first ten lines of a string?

From Dev

How can I use Perl default array variable @_ with push?

From Dev

Perl RE Matching: How can I use a variable for RE flags?

From Dev

In Perl, how can I pass a variable as an argument to a subroutine?

From Java

How can I print error and other counts in Scrapy as integer variable?

From Java

How can I print variable and string on same line in Python?

From Dev

How can I print a variable with padded center alignment?

From Dev

How can I print this?

From Dev

I have variable which asks for users input, how can i print that variable also using if/else in python?

From Dev

How can I use a Perl variable in my Log::Log4perl config file?

From Dev

How can I use a Perl variable in my Log::Log4perl config file?

From Dev

How do I print multiple variables in perl?

From Dev

How can I use perl debuggers pretty print for a hash in normal program code?

From Dev

How can I print only values in perl, result returned from mongodb?

From Dev

Can I autobox a single variable in perl?

From Dev

How can I assign a variable's content to a variable's name in perl?

From Dev

How can I set a Java-like "static variable" in Perl that can be accessed by other classes ?

From Dev

Re: How can I escape meta-characters when I interpolate a variable in Perl's match operator?

From Dev

How can I print a Maybe?

From Dev

How can I print 413284921265094656?

From Dev

How can I print 413284921265094656?

From Dev

in Perl, how to redirect a "print" to a variable rather than display on the screen

From Dev

How can I write C++ code to print variable values in python prompt with SWIG

From Dev

How can I apply a list as a variable number of arguments to the python print built-in?

Related Related

  1. 1

    How can I print a multidimensional array in Perl?

  2. 2

    How can I print the address of a bool variable?

  3. 3

    How can I print a variable to a file?

  4. 4

    How can I print a variable to stdout in Postscript

  5. 5

    How can i print the value of a variable to a Textview

  6. 6

    How can I make Perl print first ten lines of a string?

  7. 7

    How can I use Perl default array variable @_ with push?

  8. 8

    Perl RE Matching: How can I use a variable for RE flags?

  9. 9

    In Perl, how can I pass a variable as an argument to a subroutine?

  10. 10

    How can I print error and other counts in Scrapy as integer variable?

  11. 11

    How can I print variable and string on same line in Python?

  12. 12

    How can I print a variable with padded center alignment?

  13. 13

    How can I print this?

  14. 14

    I have variable which asks for users input, how can i print that variable also using if/else in python?

  15. 15

    How can I use a Perl variable in my Log::Log4perl config file?

  16. 16

    How can I use a Perl variable in my Log::Log4perl config file?

  17. 17

    How do I print multiple variables in perl?

  18. 18

    How can I use perl debuggers pretty print for a hash in normal program code?

  19. 19

    How can I print only values in perl, result returned from mongodb?

  20. 20

    Can I autobox a single variable in perl?

  21. 21

    How can I assign a variable's content to a variable's name in perl?

  22. 22

    How can I set a Java-like "static variable" in Perl that can be accessed by other classes ?

  23. 23

    Re: How can I escape meta-characters when I interpolate a variable in Perl's match operator?

  24. 24

    How can I print a Maybe?

  25. 25

    How can I print 413284921265094656?

  26. 26

    How can I print 413284921265094656?

  27. 27

    in Perl, how to redirect a "print" to a variable rather than display on the screen

  28. 28

    How can I write C++ code to print variable values in python prompt with SWIG

  29. 29

    How can I apply a list as a variable number of arguments to the python print built-in?

HotTag

Archive