Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Problem with tie *STDIN, 'IO::Scalar', \$text;

by chromatic (Archbishop)
on Apr 29, 2001 at 01:45 UTC ( #76418=note: print w/replies, xml ) Need Help??


in reply to Problem with tie *STDIN, 'IO::Scalar', \$text;

The perldoc perltie has some good examples of how to do this. I'd start with Tie::Handle, subclass it, and go from there.

You could probably get by with overloading TIEHANDLE() and READLINE(), though your needs will determine that. I can provide code if you really need some, but it's pretty plain in perltie.

Update: Perhaps I've not made myself clear. Yes, I've found myself in exactly the same situation you describe. Trying to integrate HTTP::Daemon with CGI is a hassle -- HTTP::Daemon produces a HTTP::Response object that allows you to get at posted data via the content() method (I may have the names slightly wrong, but the idea is the same). CGI reads from STDIN or another filehandle.

The solution was to make a tied filehandle, pass it to the CGI.pm constructor. This let me intercept the READ and READLINE calls from CGI.pm to the filehandle, pulling data out of content(), manipulate them as desired, and pass them along.

As far as HTTP::Response knew, I was doing normal stuff. As far as CGI.pm knew, it was reading from a normal filehandle.

Yes, it would be nice if they knew how to play together. At the cost of writing 30 lines of glue code, I faked them out.

That is why I brought it up -- it will let you do just what you describe.

  • Comment on Re: Problem with tie *STDIN, 'IO::Scalar', \$text;

Replies are listed 'Best First'.
Re: Re: Problem with tie *STDIN, 'IO::Scalar', \$text;
by Rudif (Hermit) on Apr 29, 2001 at 19:04 UTC
    Hi chromatic

    At some other time I will probably follow your advice and learn how to subclass Tie::Handle to solve some specific problem.

    At this stage I am raising what to me looks like a general perl programming question:

    Some popular perl modules export subroutines that read text from specified input file(s), defaulting to STDIN, transform the text, and deliver the result to a specified output file, defaulting to STDOUT. This is fine in many applications.

    Two subroutines that behave like this, and that I'm interested in right now are
    Pod::Html::pod2html Pod::Select::podselect

    However, I want to call these subroutines in a context where the input text or output text, or both, are in my script's scalar variables. This lets me do some other processing before calling one of these subroutines, and some more after the call.

    I would like to know if other monks have had this of requirement or wish.

    While I can easily solve my problems by using temporary files, out of curiosity I started looking for a more perlish solution. Following stephen's advice, I was able to redirect podselect STDOUT into a IO::Scalar tied to a scalar variable (see the first snippet in my post above). However, my attempt to use a similar redirection on STDIN to pod2html failed. I will try to dig deeper to try to understand why does it work when it works, and why not when not.

    Rudif

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2020-09-29 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (146 votes). Check out past polls.

    Notices?