0
votes

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} );
    }
}
2
"Can't use string as a HASH ref..." "Can't use string as an ARRAY ref..." Which is it? And what line is the error on? - user94559
I posted it foreach (@arrFiles) { #Line 247 - Reed
Can't use string ("Server/Systems/Handlers/Buddies."...) as an ARRAY ref while "strict refs" in use at Server/ClubPenguin.pm line 247 is the correct error - Reed
How would that possibly work? You're not loading that module at runtime (at least, not in the code you posted), and I doubt you're loading it at compile time. The error message is pretty clear: perhaps you forgot to load "Buddies"? - Matt Jacob
And, like @smarx mentioned in his comment, the loading of the module is a separate issue. - Matt Jacob

2 Answers

1
votes

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

0
votes

I think you need just this:

foreach (@arrFiles) {
    my $strClass = basename($_, '.pm');
    my $objSystem = $strClass->new($self);
    $self->{systems}->{$strClass} = $objSystem;
}