Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

The best way to filter file I/O

by steves (Curate)
on Nov 06, 2004 at 13:23 UTC ( [id://405759]=perlquestion: print w/replies, xml ) Need Help??

steves has asked for the wisdom of the Perl Monks concerning the following question:

I've had a general need several times to filter file I/O against existing opened file handles. The approach I've generally used is to create tie packages and tie the file handle. One issue with that is how to give the tie package the real file handle to use.

As a simple example, suppose I want to create a tie that upper cases everything either written to or read from a file handle. In order to read or write the data once it's converted, I need to pass the tie package the file handle being tied. However, if I use that file handle as is, it's tied by the time I use it internally, which causes me to recursively call back into the tie package. Not good. I can dup the file handle internally in the package, but to do that I need to know if it's opened for read or write. That leaves me with having to dup it before I tie it, then passing the dup'ed file handle as an argument.

Am I missing some better way to handle this?

Replies are listed 'Best First'.
Re: The best way to filter file I/O
by Limbic~Region (Chancellor) on Nov 06, 2004 at 14:21 UTC
    steves,
    I think you probably want to subclass PerlIO::via. I have only used it once, but there is a more complete example in TFM (just need to change hex stuff to uc).

    Cheers - L~R

Re: The best way to filter file I/O
by steves (Curate) on Nov 06, 2004 at 16:15 UTC

    Good pointer -- Thanks. But I'm not seeing how that could be used on an already open file handle. This still may fit some of my specific needs better than a tie.

      steves,
      I almost missed this as you didn't reply directly to my node. So for some reason, you won't know at the time you open the file if you are going to need to ucase it or not. Probably the easiest thing to do this then is to tie all your filehandles up front. Default behavior is a normal filehandle, but you would add a method to toggle ucase which could be called at any time.

      Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://405759]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-18 14:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found