#!/usr/bin/perl -w use strict; BEGIN { my @packages = qw(test test2); my @vars = qw(first second); populate(); sub populate { my $text = ''; foreach my $package (@packages) { foreach my $var (@vars) { $text = '$' . $package . '::' . $var . ' = 1;' . "\n"; print $text; eval $text; } } } } print "test:\t$test::first\t$test::second\n"; print "test2:\t$test2::first\t$test2::second\n"; #### #!/usr/bin/perl -w use strict; my @vars = qw(one two three); package main; my ($one, $two, $three) = (1 .. 3); { no strict 'refs'; foreach my $var (@vars) { *{"test::$var"} = $var; } } package test; print "One: $one\tTwo: $two\tThree: $three\n";