Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Using the contents of

by Popcorn Dave (Abbot)
on Sep 22, 2008 at 18:37 UTC ( [id://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

Replies are listed 'Best First'.
Re^2: Using the contents of
by ikegami (Patriarch) 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.)

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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found