in reply to Re: Perl Module Problems
in thread Perl Module Problems

Using most of your suggestions I have fixed up my Module and made it give no errors when run using the debugger. I am receiving this error however from the testing script:
Can't use string ("cstrike") as an ARRAY ref while "strict refs" in us +e at HLDS.pm line 34 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string ("cstrike") as an ARRAY ref while "strict ref +s" in use at HLDS.pm line 34. HLDS::GameMaps('cstrike') called at test.pl line 11
When I leave the string unquoted it gives this error:
Bareword "cstrike" not allowed while "strict subs" in use at test.pl l +ine 11. Execution of test.pl aborted due to compilation errors (#1) Uncaught exception from user code: Bareword "cstrike" not allowed while "strict subs" in use at t +est.pl line 11. Execution of test.pl aborted due to compilation errors.
I am not referring to an array, just passing an argument as a string. What to do?

Replies are listed 'Best First'.
Re^3: Perl Module Problems
by ikegami (Patriarch) on Sep 23, 2006 at 21:12 UTC
    That error is caused by @$game. $game is treated as a reference, but it contains a string. This is known as a symbolic reference. Symbolic references are disallowed by use strict; (or more specifically, use strict 'vars';) for the reasons explained in Why it's stupid to `use a variable as a variable name'.
      I have since changed my module because of your suggestions in your previous post. I don't think the error is due to the @$game symbolic reference. I use only the array named @maps in my sub now. Here it is:
      use strict; use warnings; use diagnostics; package HLDS; use Exporter (); our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT = (); our @EXPORT_OK = qw(array); # Create Map Array given an argument. sub GameMaps { # Declare Variables my ($game) = @_; my $user = $ENV{'USER'}; # Create Dir Path my $dir = "/home/$user/hlds_l/$game/maps/"; # End if dir not exist return unless -d $dir; #Read maps from directory my @maps; opendir(my $maps_dh, $dir) || die("Cannot open directory"); @maps = readdir($maps_dh); closedir($maps_dh); # Take only maps and trim .bsp @maps=grep /\.bsp/, @$game; foreach my $map (@maps) { $map=~ s/\.bsp// } return @maps; } 1; # end
        @maps=grep /\.bsp/, @$game; ^^^^^^ should be @maps

        Off-topic, that regexp will accept the file "file.bsp.bak". That's why I added \z to the regexp in my code.