Reputation: 281
I've been trying to load files out of two folders, Handlers
and Games
, that are located in the System
folder.
It gives me an error
Can't use string ("Server/Systems/Handlers/Buddies."...) as an ARRAY ref while "strict refs" in use at Server/ClubPenguin.pm line 247`
Here is the code
method loadSystems {
my @arrHandlers = glob('Server/Systems/Handlers/*.pm');
my @arrGames = glob('Server/Systems/Games/*.pm');
my @arrFiles = ( @arrHandlers, @arrGames );
foreach (@arrFiles) { #Line 247
foreach my $files ( @{$_} ) {
my $strClass = basename( $_, '.pm' );
my $objSystem = $strClass->new($self);
$self->{systems}->{$strClass} = $objSystem;
}
}
my $sysCount = scalar( keys %{ $self->{systems} } );
if ( $sysCount > 0 ) {
$self->{modules}->{logger}->
output( 'Successfully Loaded ' . $sysCount . ' Systems', Logger::LEVELS->{inf} );
}
else {
$self->{modules}->{logger}->
output( 'Failed To Load Any Systems', Logger::LEVELS->{err} );
}
}
Upvotes: 0
Views: 2784
Reputation: 126772
The issue is that this line
my @arrFiles = ( @arrHandlers, @arrGames );
just combines the contents of the two arrays @arrHandlers
and @arrGames
intoi a single array. It doesn't create an array of two references, as you seem to expect
You need to write just
for ( @arrFiles ) {
...
}
There's also no need to copy the original arrays, or even to store the result of the glob
calls. You can write
for ( glob 'Server/Systems/{Handlers,Games}/*.pm' ) {
...
}
with the same effect
Upvotes: 1
Reputation: 60153
I think you need just this:
foreach (@arrFiles) {
my $strClass = basename($_, '.pm');
my $objSystem = $strClass->new($self);
$self->{systems}->{$strClass} = $objSystem;
}
Upvotes: 0