views:

88

answers:

4

I'm working on a Perl script where the user adds a number of set variables at the beginning of the script, all prefixed with $XX, as seen below. The user-set variables, however, need to go through a short transformation function to clean them up.

Is there a way to run the sub on all the variables with the $XX prefix?

my $XXvar1 = "something";
my $XXvar2 = "something";
my $XXvar3 = "something";
my $XXvar4 = "something";

sub processVar {
    my $fixVar = $_[0];
    # Do stuff
    return $fixVar;
}

# This obviously doesn't work. Use some kind of loop or something? How...
$XXvar* = processVar($XXvar*);

Edit: I'm trying to do this now with a hash, as per some suggestions on Google:

my %XX;

$XX{var1} = "something 1";
$XX{var2} = "something 2";
$XX{var3} = "something 3";
$XX{var4} = "something 4";

I can then work with the keys and values in for or while loops. However, how can I reassign each variable to the transformed one in the loop?

Edit again: Got it. This for loop processes all the variables successfully:

for my $key ( keys %XX ) {
    $XX{$key} = processVar($XX{$key});
}

I'm definitely going to try to make a configuration file now, though, as suggested below. Now I just have to figure that out :)

+1  A: 
#!/usr/bin/perl

use strict;
use warnings;

my $XXvar1 = "something";
my $XXvar2 = "something";
my $XXvar3 = "something";
my $XXvar4 = "something";

my @values;
for ( 1 .. 4 ) {
    push @values, eval '$XXvar' . $_;
}

processVar(@values);

sub processVar {
    print "@_";
}
Alan Haggai Alavi
+10  A: 

Instead of users editing the source and providing odd variable names, use a configuration file instead. Every user can get his own configuration file. There are several modules on CPAN to handle configuration files of just about any format, and I talk about ways to configure Perl programs in a chapter of Mastering Perl. It's certainly a lot easier than the tricks you'd need to do to magically pick up these variable names.

brian d foy
Ape-inago
here's link to a modified version for that chapter http://www252.pair.com/comdog/mastering_perl/Chapters/11.configuration.html
melaos
+2  A: 

Read MJD's:

  1. Why it's stupid to `use a variable as a variable name'
  2. Part 2
  3. Part 3
Sinan Ünür
+1 You had me at "Read MDD's"
Telemachus
Thanks. ITYM MJD ;-)
Sinan Ünür
Yet another proof that comments should be editable. (As is, you can only delete and then add another. But if I did that here, my comment would be under your response, and that wouldn't make any damn sense. So my typo remains forever. Feh.)
Telemachus
+1  A: 

Just for the fun of it. But don't do this it's evil :-)

use PadWalker qw(peek_my peek_our peek_sub closed_over);
my $XXvar1 = "something1a";
my $XXvar2 = "something2b";
my $XXvar3 = "something3c";
my $XXvar4 = "something4d";
my $noXXvar = "something5e";

#remove last character
sub clean {
  my $h = peek_my(1);
  while(my ($key, $value) = each(%$h)) {
    if ($key =~ /^\$XXvar.+/) {
       chop ${$h->{$key}}
    }
  }
}
clean();

print "$XXvar1\n";  //something1
print "$XXvar2\n";  //something2
print "$XXvar3\n";  //something3
print "$XXvar4\n";  //something4
print "$noXXvar\n"; //something5e

My Perl is a bit rusty so there surly is a shorter/cleaner version of it.

jitter