|No such thing as a small change|
Inline::Filesby ariels (Curate)
|on Jul 23, 2001 at 17:41 UTC||Need Help??|
Item Description: Multiple virtual files at the end of your code
Another of the (good) Dr. Damian Conway's modules, Inline::Files is another tour de force of Perl source code filtering, symbol table manipulation, overridden builtins and a wholly outlandish yet useful concept, wrapped in an entirely Perlish syntax.
(In case you're wondering, yes, I like the module)
A word of warning!
If you're a "hands-on" person, you might want to install and use this module before finishing to read the documentation or this review. PLEASE be careful, and use the -backup flag to the module, at least until you understand what it does! Say "use Inline::Files -backup" instead of "use Inline::Files"!
What does it do?
Ever needed to package a few small files along with your program? Well, you can use DATA. But that just gives you one "file". And it's always on the DATA filehandle, which might not suit your programming style.
Or you can use the "here-document" syntax to get at some strings in your program. That gives you as many strings as you like, but you can't use file operators to read them. And changing them can be tricky (see below for just how easy Inline::Files makes it to change your files...).
Inline::Files lets you read "virtual files" which you enclose in your program's source. At BEGIN time it does its magic, and you have some new files available to you.
At the end of your program, you can put any number of what Inline::Files calls virtual files. These are just regions of you text beginning with special markers __YOUR_CHOICE_OF_ID__. You can then read the contents of this "virtual file" by reading the filehandle <YOUR_CHOICE_OF_ID>. Note that YOUR_CHOICE_OF_ID is the name of a filehandle, so it should be in UPPER CASE, or it won't be recognised.
More than one virtual file can be associated with the same ID; reading from the filehandle will retrieve them in order.
More information is available about these virtual files. In particular, you can get a list of the "virtual file names" associated with each ID; open is overloaded to know how to open these files by their names. You can also get the name of the current file being read by the ID, as well as some more esoteric information.
This code arranges 2 blocks of text in a nice manner; the text is read directly from a virtual file, which is nicer than having to split on all newlines of a string.
It's also a nice way to test some code you want to write to run on real filehandles, while keeping everything in one file.
Not content with letting your programs read parts of themselves in weird and wonderful ways, Dr. Conway also lets your programs write those parts!
If your program file is writable, all its virtual files are opened in read-write mode. To rewrite the file, just seek back to the beginning and rewrite it. (Or be more creative if you like.) You can even create new virtual files as your program is running.
This is a cheap and useful way of keeping some "sticky" configuration data attached to your Perl scripts (note, however, that this is per-script or per-module configuration data, rather than per-user or per-instance data).
See the fine manual and the examples provided with the module in the demo/ subdirectory for more examples.
What -backup does
If your program is writing virtual files, you should be very afraid: Your program is getting rewritten to disk continually. -backup makes a backup copy of your program when it loads, just in case. Use it if you're writing to virtual files!
Inline::Files is a pure Perl extension, and doesn't require a C compiler to install. It does require that you have Filter::Util installed for your Perl, and that extension contains some C files.