in reply to Perl Module Problems
In scalar context, an array returns the number of elements it contains. That means my $game = @_; assignes the number of arguments (1) to $game. Force the assignment into a list context.
my $game = @_;
should be
my ($game) = @_;
All instances of @$game in your sub should be @maps. Don't forget to do my @maps;. There's no reason for the array to be named after the game, which is what you are trying to do with symbolic references (which are almost always bad and causing some of your errors).
array is an aweful name for that sub. What about get_game_maps?
my $map; foreach $map (@maps)
can be written more concisely as
foreach my $map (@maps)
Your function returns a list of maps, but you discard the return value.
MAP::array("cstrike");
should be
my @cstrike_maps = MAP::array("cstrike");
Congrats for using use strict;. You should also use use warnings;. Warnings almost always point to an error. ( Oops! I see you used -w. I'd put use warnings; in the module explicitely. You shouldn't rely on the calling script using -w. )
use vars qw( @ISA ); @ISA = qw( Exporter );
can be written more concisely as
our @ISA = qw( Exporter );
Handle MAPS should be localized. Better yet, it should be a lexical (my) variable.
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 | |
by davidov0009 (Scribe) on Sep 23, 2006 at 19:58 UTC | |
by ikegami (Patriarch) on Sep 23, 2006 at 21:03 UTC | |
|
Re^2: Perl Module Problems
by davidov0009 (Scribe) on Sep 23, 2006 at 20:19 UTC | |
by ikegami (Patriarch) on Sep 23, 2006 at 21:12 UTC | |
by davidov0009 (Scribe) on Sep 24, 2006 at 04:06 UTC | |
by ikegami (Patriarch) on Sep 24, 2006 at 04:17 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |