in reply to initializing multiple arrays

Just for fun, I was playing with creating interpolated variables and attempting to actually create a series of arrays called "@array00" .. "@array09". I thought that by using "use vars", "vars->import()" I could accomplish this with strict/warnings turned on.

This was my test:

#!/usr/bin/perl use strict; use warnings; use vars; my @array_references; for(0..9) { $array_references[$_] = [1..100]; } for(0..9) { my $aname = sprintf('array%02d', $_); vars->import("\@$aname"); @$aname = @{$array_references[$_]}; } print "@array01";

Which doesn't work. If I remove the strict/warnings section, it in fact does create the arrays and work as expected.

But how do you accomplish this (regardless of whether it's stupid or not) and keep warnings/strictness?

mr.nick ...

Replies are listed 'Best First'.
Re^2: initializing multiple arrays
by boftx (Deacon) on May 30, 2014 at 20:47 UTC

    You don't, unless you turn off strict refs:

    # since strict is a lexical pragma you can put it in a block and let # it return to its former state when you leave the block. { no strict refs; # do something that might be ill-advised ... }
    This is, in my experience, most commonly done to auto-generate accessor methods. But turning off strict refs is generally frowned upon. That is why I suggested using a hash to store the names as keys that can be computed.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.