Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


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.

In reply to Inline::Files by ariels

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-12-09 15:08 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.