Perl question for you all. I have the following code:
#! /usr/local/bin/perl
use warnings;
use strict;
use Switch;
our @ITEMS_ARRAY = qw ( a b c );
foreach my $i (0 .. $#ITEMS_ARRAY) {
switch ($ITEMS_ARRAY[$i]) {
case "a" { print "a\n" }
case "b" { print "b\n" }
case "c" { print "c\n" }
}
}
which works fine. I need to set up "@ITEMS_ARRAY" differently for certain cases. Like the following:
#! /usr/local/bin/perl
use warnings;
use strict;
use Switch;
our $X = 1;
if ($X) {
our @ITEMS_ARRAY = qw ( a b c );
} else {
our @ITEMS_ARRAY = qw ( e f g );
}
foreach my $i (0 .. $#ITEMS_ARRAY) {
switch ($ITEMS_ARRAY[$i]) {
case "a" { print "a\n" }
case "b" { print "b\n" }
case "c" { print "c\n" }
case "e" { print "e\n" }
case "f" { print "f\n" }
case "g" { print "g\n" }
}
}
But, I get the following errors:
Variable "@ITEMS_ARRAY" is not imported at ./r.pl line 14.
Variable "@ITEMS_ARRAY" is not imported at ./r.pl line 15.
Global symbol "@ITEMS_ARRAY" requires explicit package name at ./r.pl line 14.
Global symbol "@ITEMS_ARRAY" requires explicit package name at ./r.pl line 15.
Execution of ./r2.pl aborted due to compilation errors.
Any help would be greatly appreciated!
Thanks!
use Switch, ever. 2) Declare@ITEMS_ARRAYoutside of the block.Switchsays, do not use if you can use given/when.givenandwhenwere introduced in version v5.10 of Perl together with smart matching, but you shouldn't use them either as they're experimental features, and you would frustrate people if you asked for help with code that uses them as everybody else is avoiding them and they would have to read the documentation to help youour, except forour @ISAandour @EXPORT_OK. Usemy