Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using the contents of

by Popcorn Dave (Abbot)
on Sep 22, 2008 at 18:37 UTC ( #713069=note: print w/ replies, xml ) Need Help??


in reply to Using the contents of

Thanks for your replies!

I'm just curious though if there's a way to do that generically so that when a file was called for that you could reference some other data stream that had been generated rather than having to write data to a temporary file.


Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

I would love to change the world, but they won't give me the source code


Comment on Re: Using the contents of
Re^2: Using the contents of
by JadeNB (Chaplain) on Sep 22, 2008 at 18:53 UTC
    You could tie the filehandle to a scalar, implementing the READLINE, GETC, and READ methods appropriately. See Tying Filehandles. (I thought there must be a CPAN module to do this—sort of the converse of Tie::File—but a cursory search didn't find it.)

    UPDATE: IO::String and IO::Stringy both seem to be the sort of thing that you'd want here. The documentation suggests that they're only useful on older versions that don't support lexical filehandles, but it seems to me that lexical filehandles don't suffice for your situation.

    UPDATE 2: ikegami pointed out what I was missing (i.e., why you shouldn't tie after all). Thanks!

Re^2: Using the contents of
by ikegami (Pope) on Sep 22, 2008 at 19:56 UTC
    Don't use tie. Since 5.8.0, you can create a file handle that reads from/writes to a scalar.
    my $data = '...'; open(my $fh, '<', \$data) or die; sub_that_needs_file_handle($fh);

    Before 5.8, IO::Scalar and IO::String provide the functionality (although not as well) by using tie

    Neither tie nor this method produce a real (system) file handle, so they won't work where a real file handle is needed. For example, you can't use them to capture the output of a child process. (See IPC::Run for a solution in that particular case.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-10-25 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls