Rudif has asked for the wisdom of the Perl Monks concerning the following question:
Ave!
stephen showed us recently here how to capture into a scalar variable the output that a Perl subroutine, such as Pod::Html::pod2html or Pod::Select::podselect, writes to STDOUT :
Is it possible, and how could I do it?
I tried this
My script below shows the following:
My platform is Win2k and perl is 5.6.0, AS build 623.
Rudif
stephen showed us recently here how to capture into a scalar variable the output that a Perl subroutine, such as Pod::Html::pod2html or Pod::Select::podselect, writes to STDOUT :
Now I want to achieve the opposite: feed text from a scalar variable to a subroutine that reads from STDIN.my $podfile = "some.pod"; my $scalar = ''; tie *STDOUT, 'IO::Scalar', \$scalar; podselect $podfile; # reads form $podfile, writes to STDOUT untie *STDOUT; # now $scalar contains the pod from $podfile
Is it possible, and how could I do it?
I tried this
but it does not work as I expected. Am I doing something wrong, or is this a limitation of IO::Scalar?my $scalar = "some pod text"; tie *STDIN, 'IO::Scalar', \$scalar; pod2html; untie *STDIN; # reads from 'real' STDIN, not from $scalar
My script below shows the following:
- tieAngle(): tie *STDIN, 'IO::Scalar', \$text; works as I expected when used with the angle operator <>
- pipePod2html(): a workaround - I can print to a pipe, but only if the subroutine is wrapped in an executable script (pod2html.pl or pod2html.bat on Win32)
- tiePod2html(): tie *STDIN, 'IO::Scalar', \$text; does not work with pod2html
- tieOpenDash(): tie *STDIN, 'IO::Scalar', \$text; does not work when I emulate what pod2html does (it opens a file handle to read from STDIN and reads from it).
My platform is Win2k and perl is 5.6.0, AS build 623.
Rudif
#! perl -w use strict; use Pod::Html; use IO::Scalar; $|++; my $text = <<EOT; =head1 TESTING First line Second line Third line =cut EOT # these work tieAngle(); pipePod2html(); # these do not work tiePod2html(); tieOpenDash(); sub tieAngle { print "\n=1 tieAngle================================== OK\n"; tie *STDIN, 'IO::Scalar', \$text; while (<STDIN>) { print "=1=$_"; } untie *STDIN; } sub pipePod2html { print "\n=pipePod2html================================== OK\n"; open PIPE, "| pod2html"; # works, because there is a pod2html.bat print PIPE $text; } sub tiePod2html { print "\n=tiePod2html================================== NOT OK\n"; tie *STDIN, 'IO::Scalar', \$text; pod2html; untie *STDIN; } sub tieOpenDash { print "\n=3 tieOpenDash================================== NOT OK\n +"; tie *STDIN, 'IO::Scalar', \$text; # similar to what pod2html does: open TSCLR, "<-" or die "can't open <-"; while (<TSCLR>) { print "=3=$_"; } close TSCLR; untie *STDIN; } __END__
Back to
Seekers of Perl Wisdom