Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Perl as a command executor (with hash variable substitution)

by choroba (Canon)
on Mar 22, 2013 at 15:10 UTC ( #1024941=note: print w/ replies, xml ) Need Help??


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
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Perl as a command executor (with hash variable substitution)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024941]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2015-07-30 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls