Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
No such thing as a small change
 
PerlMonks  

Inline::Files

by ariels (Curate)
on Jul 23, 2001 at 17:41 UTC ( #99012=modulereview: print w/ replies, xml ) Need Help??

Item Description: Multiple virtual files at the end of your code

Review Synopsis:

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.

Virtual files

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.

An example

#!/usr/local/bin/perl -w use Inline::Files; my ($a,$b,$a_end,$b_end); for($a=<A>, $b=<B>; ! $a_end || ! $b_end; ($a_end or $a=<A>), ($b_end or $b=<B>)) { $a_end=1, $a='' unless defined $a; $b_end=1, $b='' unless defined $b; chomp($a); chomp($b); printf "%35s | %35s\n", $a, $b; } __A__ This is a block of text. Note that this text had best have *SHORT* lines, or we could have some formatting trouble! __B__ The quick brown fox jumps over the lazy dog. The cow jumps over the moon.
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.

Writing

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!

Prerequisites

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.

Comment on Inline::Files
Select or Download Code

Back to Reviews

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls