Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Tricking modules into thinking scalars are globs. (code)

by deprecated (Priest)
on May 08, 2001 at 03:58 UTC ( [id://78724]=perlquestion: print w/replies, xml ) Need Help??

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

Lets start with some code:
my $handle = gensym; open $handle, ref ($file) ? ">&". fileno ($file) : ">" . $file and binmode ($handle) or goto &_drat;
This is from Archive::Tar, around line 750, for those who like to read along. This has been my absolute arch nemesis in this project. It wants a steenkin' file. And it will not accept anything but a file. Some of you may have read Self Extracting Archives with Perl (almost) a while ago. I had at the time abandoned using Archive::Tar. However, I am doing a similar thing now and I must use tar. So I'm going over my code again, and updating it with the knowledge I've learned since then, and I switched to IO::String from IO::Scalar due to this bit in the IO::Scalar pod:
    the IO::String manpage, which is quite similar but which was designed more-recently and with an IO::Handle-like interface in mind, so you can mix OO- and native- filehandle usage without using tied(). Note: if anyone can make IO::Scalar do that without breaking the regression tests, I'm all ears.
and this bit from the Archive::Tar pod:
    write ($file, $compressed) Write the in-memory archive to disk. The first argument can either be the name of a file or a reference to an already open file handle (be a GLOB reference). If the second argument is true, the
So in theory, IO::String allows me to create a pseudofile in memory that I can then print stuff to as if it were a file. My hope was that Archive::Tar wouldn't know any better, and would happily print to it, and I could then steal its tarball and do cool stuff to it (like MIME::Base64 or Convert::UU or Compress::Gzip, et cetera), followed by even more cool stuff (like Mail::Mailer, among other things)

Mmmmkay, let's look at some errors.

    Can't locate object method "FETCH" via package "IO::String" at /home/p +erl/lib/5.6.0/ppc-linux-thread-multi/Data/Dumper.pm line 150.
is gleaned from the following code:
    tie *IOST, 'IO::String'; print Dumper \*IOST;
So that's kind of sucky. But I found out that the FETCH thing I'm looking for is over in perldoc perltie, and it seems like a real pain in the ass.
    FETCH This method will be triggered every time the tied variable is accessed (read). It takes no arguments beyond its self reference, which is the object representing the scalar we're dealing with. Because in this case we're using just a SCALAR ref for the tied scalar object, a simple $$self allows the method to get at the real value stored there. In our example below, that real value is the process ID to which we've tied our variable.
So this all boils down to one thing. I have a module that wants to write to a filehandle. But I dont want to write to the disk at all. I am just going to pass it to something else, and I dont see why I should have to write out a file and read it back in. That just seems stupid. But all the seemingly close-enough-to-be-useful methods are a) very complicated or b) not the solution.

If Larry were to fix this for me today, it would be thusly:

open TRICKERY, \$stringamabob;
Which 'works', but actually doesnt. (try it and see).

I guess the other solution would be to steal Archive::Tar's data. That doesnt seem particularly elegant, and I'd rather have a perl solution to this.

Finally, since this was rather information-rich, I pondered putting it in Meditations. However, I didnt really produce anything worth meditating on, so it resides in SoPW for now. Feel free to edit if if you feel the urge.

whiiiiiiiiine,
brother dep.

--
Laziness, Impatience, Hubris, and Generosity.

Replies are listed 'Best First'.
(bbfu) Re: Tricking modules into thinking scalars are globs. (code)
by bbfu (Curate) on May 08, 2001 at 05:31 UTC

    Have you tried using a pipe? It's maybe not the best solution since (I think) it still uses OS io calls but... *shrug* It would at least let you not have to create a file-on-disk.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re: Tricking modules into thinking scalars are globs. (code)
by lindex (Friar) on May 08, 2001 at 19:03 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found