Ryan Fuhrman Ryan Fuhrman - 3 months ago 13
Perl Question

Can't use string as a ARRAY ref while "strict refs" in use

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} );
}
}

Answer

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

Comments