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?
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!In reply to H.O.P && aXML by Logicus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |