in reply to Re: Need Help: Capture Print Outputs from a Function
in thread Need Help: Capture Print Outputs from a Function

It does not seem to be working. I get the following message: Use of uninitialized value in concatenation (.) or string at scop/x.pl line 126. buffer: Line 126 is where print "buffer: $buf\n"; is. Also, what is the structure under my ($buf); in your code? Is that how PERL creates a macro or what C calls prototypes? I've also tried the first method mentioned above and that also fail to initialize the variable $var. In that first function, what is the significant of "qw"? Thanks.
  • Comment on Re^2: Need Help: Capture Print Outputs from a Function

Replies are listed 'Best First'.
Re^3: Need Help: Capture Print Outputs from a Function
by ikegami (Patriarch) on May 02, 2006 at 18:19 UTC
    I get the following message: Use of uninitialized value in concatenation (.)

    You're using a version of Perl older than 5.8.0, when open(STDOUT, '>', \$buf) was introduced. Before 5.8, \$buf would be stringified to something like SCALAR(0x1ab2760). The output ends up in a file by that name instead of going to $buf, so $buf is still undefined when you go to inspect it.

    Before 5.8, you had to use IO::String or IO::Scalar.

    Also, what is the structure under my ($buf); in your code?

    It creates a new scope. This causes local *STDOUT; to be undone (thus restoring the original STDOUT) before the last print.

    what is the significant of "qw"

    qw[ some args here ]
    is the same thing as
    ('some', 'args', 'here')
    See perlop.

Re^3: Need Help: Capture Print Outputs from a Function
by wfsp (Abbot) on May 02, 2006 at 18:22 UTC
    I updated my node to point out that perl 5.8 is required for that to work (apologies for the delay).

    Which version of perl are you using?

      wfsp.

      Thank you for your patience. Being a beginner, I appreciate yours and everyone else's help and patience.

      Anyway, the code is working as a stand alone so I must be introducing something else that it does not like.

      My PERL version is listed as 5.008005 in SunOS 5.8.

      On the subject of a new scope. Does that mean I should do the following:

      sub1() { my $variables; my $buff; code code code { local *STDOUT; open( STDOUT, '>', \$buf ) or die "Write to buffer ailed\n"; mysub(); } print "buffer: $buf\n"; more code } sub mysub { print "mysub output\n"; }
      Again, thanks. MNJ