Search code examples
perlreferenceperl-data-structures

perl can't use string as an array ref


I have 4 apps. let's call them: App1, App2, App3 and App4.

for each of these apps I have an array: for example:

my @App1_links = (...some data...);
my @App2_links = (...some data...);
my @App3_links = (...some data...);
my @App4_links = (...some data...);

Now I have a loop in my code that goes thru these 4 apps and I intend to do something like this:

my $link_name = $app_name . "_links";
    where $app_name will be App1, App2 etc...

and then use it as : @$link_name

Now this code does what I intend to do when I don't use: use strict but not otherwise

The error is: Can't use string ("App1_links") as an ARRAY ref while "strict refs" in use at code.pm line 123.

How can I achieve this functionality using use strict.

Please help.


Solution

  • You are using $link_name as a symbolic reference which is not allowed under use strict 'refs'.
    Try using a hash instead, e.g.

    my %map = (
        App1 => \@App1_links,
        ...
    );
    my $link_name = $map{$app_name};