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 :
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
Now I want to achieve the opposite: feed text from a scalar variable to a
subroutine that reads from STDIN.
Is it possible, and how could I do it?
I tried this
my $scalar = "some pod text";
tie *STDIN, 'IO::Scalar', \$scalar;
pod2html;
untie *STDIN;
# reads from 'real' STDIN, not from $scalar
but it does not work as I expected. Am I doing something wrong, or is this a limitation
of IO::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).
where 'does not work' means that it does not read from my scalar but from the real
STDIN.
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__
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.