i'm basically new to modules and i'm trying to use them in my scripts. i am having trouble finding the right way of using them properly and i'd like your advice about it.
let me explain quickly what i'm trying to do :
my script is doing some file transfers, based on data from XML files.
so basically, i have XML files with contents like that :
<fftg>
<actions>
<!-- Rename file(s) -->
<rename>
<mandatory>0</mandatory>
<file name="foo" to="bar" />
</rename>
<!-- Transfer file(s) -->
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<server>fqdn</server>
<port>22</port>
<file name="bar" remotefolder="toto" />
</transfer>
<!-- Transfer file(s) -->
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<server>fqdn</server>
<port>22</port>
<file name="blabla" remotefolder="xxxx" />
<file name="blabla2" remotefolder="xxxx" />
</transfer>
</actions>
</fftg>
in a few words, i have a script performing "actions". every action can be repeated X times.
now, instead of having an important script with a bunch of subroutines etc.. i think it should be better to create modules for my app, and put the actions in modules.
for example :
FFTG::Rename
FFTG::Transfer
FFTG::Transfer::SFTP
FFTG::Transfer::FTP
& so on (i've created all these modules and they work fine independently)
and call these modules depending on the actions specified in the XML file. people could create new modules/actions if required (i want the thing to be modular).
now, i don't know how to do this properly.
so my question is : what is the best way to do this please ?
currently, my script is reading these actions like that :
# Load XML file
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($FFTG_TSF . "/" . $tid . ".xml");
# Browse XML file
foreach my $transfer ($doc->findnodes('/fftg')) {
# Grab generic information
my($env) = $transfer->findnodes('./environment');
my($desc) = $transfer->findnodes('./description');
my($user) = $transfer->findnodes('./user');
print $env->to_literal, "\n";
# Browse Actions
foreach my $action ($doc->findnodes('/fftg/actions/*')) {
my $actiontype = ucfirst($action->nodeName());
# how do i select a module from the $actiontype here ? ($actiontype = Rename or Transfer)
# i can't do : use FFTG::$actiontype::execaction(); or something for example, it doesnt work
# and is it the right way of doing it ?
}
}
but maybe it's not the right way of thinking it. (i'm using Lib::LibXML) how can i call the module "dynamically" (using a variable in the name, such as FFTG::$actiontype for example and also, does it mean that i have to have the same subroutine in every module ? example : sub execaction
as i want to send differnt data to the module......
any hints ? thanks again regards,