in reply to construct variable name on the fly

Here's a method, but it doesn't work under strict.

{ no strict 'subs'; foreach $number(1..2) { print $var.$number . "\n"; } }

Update: Oops, a little too fast on the draw. I confused it with populating HTML::Template params. Way off!

{ no strict 'subs'; foreach $number(1..2) { $template ->param( var.$number => $somevalue ); } }

So, Fletch is absolutely correct on both accounts.


—Brad
"Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

Replies are listed 'Best First'.
Re^2: construct variable name on the fly
by pg (Canon) on Oct 19, 2004 at 02:05 UTC

    Whatever you posted before your update, or after, why no strict 'subs' has anything to do with the problem.What you want to mention is really 'refs'.

    This works:

    $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);

    This works:

    use strict('subs'); $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);

    This does not work:

    use strict('refs'); $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);
Re^2: construct variable name on the fly
by Fletch (Bishop) on Oct 19, 2004 at 00:45 UTC

    No, that doesn't work even under strict. ${ "var$number" } would, but again symbolic references are bad practice.