deprecated has asked for the wisdom of the Perl Monks concerning the following question:
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:my $handle = gensym; open $handle, ref ($file) ? ">&". fileno ($file) : ">" . $file and binmode ($handle) or goto &_drat;
and this bit from the Archive::Tar 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.
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)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
Mmmmkay, let's look at some errors.
is gleaned from the following code: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.
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.tie *IOST, 'IO::String'; print Dumper \*IOST;
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.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.
If Larry were to fix this for me today, it would be thusly:
Which 'works', but actually doesnt. (try it and see).open TRICKERY, \$stringamabob;
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 | |
Re: Tricking modules into thinking scalars are globs. (code)
by lindex (Friar) on May 08, 2001 at 19:03 UTC |