http://www.perlmonks.org?node_id=1024941


in reply to Perl as a command executor (with hash variable substitution)

Instead of using several hashes, use just one hash. Transform the old hashes' names into keys in the new hash:

evaluate.pl:

#!/usr/bin/perl use warnings; use strict; use Local::Evaluator qw(evaluate); system map { Local::Evaluator::evaluate($_) } @ARGV;

Local/Evaluator.pm

package Local::Evaluator; use warnings; use strict; use parent 'Exporter'; our @EXPORT_OK = qw( evaluate ); { my %HASH; $HASH{safe}{bin} = '/usr/bin'; $HASH{unsafe}{bin} = '/bin'; sub evaluate { my $word = shift; return $word unless 0 == index $word, '#'; my ($key, $subkey, $rest) = (split /#/, $word, 4)[1 .. 3]; return $HASH{$key}{$subkey} . $rest if $HASH{$key} and exists $HASH{$key}{$su +bkey}; return $word; } } __PACKAGE__

Usage

$ ./evaluate.pl ls -latr \#unsafe#bin#/true -rwxr-xr-x 1 root root 30204 2010-09-21 20:33 /bin/true $ ./evaluate.pl ls -latr \#safe#bin#/true ls: cannot access /usr/bin/true: No such file or directory
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ