Hi there, I am trying to write a program that will load all of the subroutines from text files into a hashref, then call them using soft references. I am using this code:
opendir( ROUTINES, './data/routines/' ) || die( 'Error Loading Routines: Unable to open directory'."\n" );
foreach my $rtn( readdir( ROUTINES) )
{
next if( $rtn =~ m/^\.{1,2}$/ );
next if( -d( './data/routines/'.$rtn ) );
open( FILE, './data/routines/'.$rtn ) or die( 'Error Loading Routines: Unable to Open File '.$rtn."\n" );
local $/ = undef;
my $data = <FILE>;
close( FILE );
my( $rtnname ) = ( $rtn=~ m/^([A-Z0-9- ]+)/i );
$prog->{'routines'}->{$rtnname} = eval( 'return( '.$data.' );' );
die( 'RoutineError: '.$@."\n" ) if( $@ );
}
closedir( ROUTINES );
Now this doesnt throw up any errors, but when I try to call a routine I get an error saying I have an unblessed reference. Now I know that it is just a hashref and not a class so the error makes sense, but how can I achieve what I am trying to do? I have done this on another project a long time ago by using the same code and then casting the hashref values as routines (eg: &{$prog->{'routine'}}()
but this doesnt seem to work any more.
Any help much apreciated
opendir( ROUTINES, './data/routines/' ) || die( 'Error Loading Routines: Unable to open directory'."\n" );
foreach my $rtn( readdir( ROUTINES) )
{
next if( $rtn =~ m/^\.{1,2}$/ );
next if( -d( './data/routines/'.$rtn ) );
open( FILE, './data/routines/'.$rtn ) or die( 'Error Loading Routines: Unable to Open File '.$rtn."\n" );
local $/ = undef;
my $data = <FILE>;
close( FILE );
my( $rtnname ) = ( $rtn=~ m/^([A-Z0-9- ]+)/i );
$prog->{'routines'}->{$rtnname} = eval( 'return( '.$data.' );' );
die( 'RoutineError: '.$@."\n" ) if( $@ );
}
closedir( ROUTINES );
Now this doesnt throw up any errors, but when I try to call a routine I get an error saying I have an unblessed reference. Now I know that it is just a hashref and not a class so the error makes sense, but how can I achieve what I am trying to do? I have done this on another project a long time ago by using the same code and then casting the hashref values as routines (eg: &{$prog->{'routine'}}()
Any help much apreciated