0

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!

6
  • 1
    1) Don't use Switch, ever. 2) Declare @ITEMS_ARRAY outside of the block. Commented Jul 27, 2015 at 20:34
  • Thanks much, appreciate the help! Commented Jul 27, 2015 at 20:42
  • @user5150183: As the documentation for Switch says, do not use if you can use given/when. given and when were 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 you Commented Jul 27, 2015 at 21:14
  • 1
    Note: There's almost no reason to ever use our, except for our @ISA and our @EXPORT_OK. Use my Commented Jul 27, 2015 at 23:12
  • Switch is very experimental module, that uses slow and potentially buggy mechanism of source filters (perl source code parsing/interpreting inside of the "main" compiled perl code - like eval "string"), so you must use given/when, or, better, hash containing link to anonymous subroutines - whenever possible. It is strongly not recommended to use Switch module in any production environment. Commented Jul 28, 2015 at 9:37

1 Answer 1

1

our is block-scoped. You need to declare outside of the block so that the variable is visible in the outer scope.

our $X = 1;
our @ITEMS_ARRAY;

if ($X) {
  @ITEMS_ARRAY = qw ( a b c );
} else {
  @ITEMS_ARRAY = qw ( e f g );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks much, got it! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.