in reply to Perl Module Problems

use strict; use warnings; use diagnostics; package MAP; use Exporter (); our $VERSION = 1.00; our @ISA = qw( Exporter ); our @EXPORT = (); our @EXPORT_OK = qw( get_game_maps ); sub get_game_maps { # Declare Variables my ($game) = @_; my $user = $ENV{'USER'}; # Create Dir Path my $dir = "/home/$user/hlds_l/$game/maps/"; # We're done if the dir does not exist. return unless -d $dir; opendir(my $maps_dh, $dir) || die("Cannot open directory"); my @maps = readdir($maps_dh); closedir($maps_dh); # Take only maps and trim .bsp @maps = grep /\.bsp\z/, @maps; foreach my $map (@maps) { $map =~ s/\.bsp\z//; } return @maps; } 1; # end
#!/usr/bin/perl5 use strict; use warnings; use diagnostics; # Load Module use MAP; # Call Routine my @cstrike_maps = MAP::get_game_maps("cstrike"); # Print returned array foreach my $map (@cstrike_maps) { print "$map\n"; }

Update: A more consice version of the sub:

use List::Util qw( apply ); sub get_game_maps { my ($game) = @_; my $user = $ENV{'USER'}; my $dir = "/home/$user/hlds_l/$game/maps/"; # We're done if the dir does not exist. return unless -d $dir; opendir(my $maps_dh, $dir) or die("Cannot open directory"); return apply { s/\.bsp\z// } grep { /\.bsp\z/ } readdir($maps_dh); }

Replies are listed 'Best First'.
Re^2: Perl Module Problems
by davidov0009 (Scribe) on Sep 23, 2006 at 19:54 UTC
    Thanks for the help. The reason it creates @$game is because this sub is used multiple times to create arrays for many different games within the same script.
      On second thought, I see why you can call the @maps array within the function because the retruned values are dumped into their own arrays within the script that calls the function.
        Bingo! When dealing with the maps of multiple games, you may want to consider a Hash of Arrays (HoA).
        my %maps; foreach my $game (qw( cstrike ... )) { $maps{$game} = [ MAP::get_game_maps($game) ]; }
Re^2: Perl Module Problems
by davidov0009 (Scribe) on Sep 23, 2006 at 20:19 UTC
    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?
      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
A reply falls below the community's threshold of quality. You may see it by logging in.