If you haven't read it already I strongly recommend reading Higher Order Perl by Mark Jason Dominus. For those of you who are familiar with the content of the book, I would like to take a minute here to discuss one of the code examples therein in the context of an aXML driven system.

If you recall, Mark created a routine he called dirwalk, which he then extended to allow for its reuse through including a custom subroutine to be handed to the dirwalk routine that causes the dirwalk algorythm to perform a given action for each file it finds in a given directory.

As a result of the simple syntax rules of aXML the same effect can be acheieved with ease in a very simple fashion.

First we create our dir-walker as a plugin, which uses a typical glob to get the list of files to traverse.

In our aXML document we want to put something like : <h1>Files</h1> <hr> <walk>/home/user/Desktop/</walk>

And as a result of the walk plugin running, we get a list of files which exist in the folder specified in the tag $data. The code for the plugin might look something like this :

my @filelist = glob($data); $result = join('<br>',@filelist); #N.B I haven't tested the above code, it's for example/illustration pu +rposes only.

This is not very useful, and not extendible, what if for instance, we wish to have a delete button next to each file or we want to see the file attributes, or we want a hyperlink to open the file instead of just getting back a list of names? What if we want to format the output without re-writing the plugin, but also want to use the same plugin elsewhere with different formatting?

This is where the black magic of the aXML syntax starts to come into play.

lets say we want a hyperlink for each file : <walk dir="/home/username/Desktop"> [link to="<d>filename</d>"]<d>filename</d>[/link]<br> </walk> or a hyperlink and a delete link : <walk dir="/home/username/Desktop"> [link to="<d>filename</d>"]<d>filename</d>[/link] - [link action="deletefile" filename="<d>filename</d>"]Delete[/link] +<br> </walk>

In each case we want to reuse the same walk function. The code which will work with both of the above examples would look something like this :

my @filelist = glob($command_args->{'dir'}); my @results; my $mask; map { $mask = $data; $mask =~ s@<d>filename</d>@$_@gs; push (@results, $mask); } @filelist; $result = join('',@results);

Ta dah! now the walk function understands that the data contained in its tag is a mask which needs to be filled out for each one of the files, and it replaces <d>filename</d> with the appropriate information for each one.

The same dir walking function will now also work for this :

<table> <walk dir="/home/username/Desktop"> <tr> <td>Filename :</td> <td><d>filename</d></td> <td>[link to="<d>filename</d>"]Show[/link]</td> <td>[link action="deletefile" filename="<d>filename</d>"]Delete[ +/link]</td> <td>[link action="archivefile" filename="<d>filename</d>"]Add to + archive[/link]</td> </tr> </walk> </table>

You can also (for want of a better example), make it so that the folder being walked is specified by a query data item by modifying the opening tag thusly :

<walk dir="<qd>dirname</qd>"> ... ... </walk>

If you want to store the mask elsewhere, you can for instance put it in a seperate file, and call an insertfile plugin to bring it forth :

<walk dir="/home/username/Desktop"> <insertfile>common/dirmask1.aXML</insertfile> </walk>

The ways that plugins can be stacked and reused using the simple ruleset of aXML and plugins which are coded well is virtually limitless!

Doc Brown : Marty! You've got to come back with me!
Marty : Where??
Doc Brown : Back to the future!!

In reply to H.O.P && aXML by Logicus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.