Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

executing hash element as command

by Sun751 (Beadle)
on Jul 02, 2009 at 08:09 UTC ( #776664=perlquestion: print w/ replies, xml ) Need Help??
Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a perl script where I am suppose do lost of copying and execution other scripts, So to achieve that I used configuration file which hold all the commands. And in my script my hash reads configuration file (some command referance name as key and command as value) And in below code I am executing one of the copy command,
sub initilize_config { my ($HR_config,$config_file) = @_; open (my $fr, '<', "$config_file") || die "Unable to open configur +ation file: $config_file $!"; while (my $line = <$fr>) { $line =~ tr/\r\n//d; next unless $line; if ($line =~ /^(.+?)=(.+?)$/) { $$HR_config{$1} = $2; } } } sub prn { my $HR_config = shift; system("$HR_config->{CMD_cp}"); if ( $? == -1) { print "Conmmand Failed\n"; } }
And I am suppose to do lots of coying, executing other scripts and removing files, So I am planning to achieve that using above method. So can any one suggest me If I am doing right or is there any other better way to do it, any improvement needed Please! Cheers

Comment on executing hash element as command
Download Code
Re: executing hash element as command
by llancet (Friar) on Jul 02, 2009 at 08:19 UTC
    I don't really understand what you want to do. May be you means to:
    foreach my $key (keys %$HR_config) { system $HR_config->{$key}; }
      That seems unlikely. That would mean he wants to execute all the commands from the configuration file, in an order that may vary from run to run.

      From both the description and the code it seems he reads in a set of commands (tagged with a name), and then further logic dictates which commands will be run. For instance, the prn sub runs the command tagged CMD_cp.

Re: executing hash element as command
by Anonymous Monk on Jul 02, 2009 at 09:10 UTC
    1. don't roll your own config file
    2. use Config::Any;
    3. don't roll your own config file
    :D
Re: executing hash element as command
by rovf (Priest) on Jul 02, 2009 at 09:33 UTC
    I am suppose do lost of copying and execution other scripts

    You mean: Your program is copying other scripts, and executing other scripts? With other scripts, you mean: Other Perl programs?

    configuration file which hold all the commands

    You mean: There is a file which contains a list of instructions of what other files need to be copied or executed? In this case, one would probably not call this a "configuration file"; a configuration file would typically contain options to configure a program.

    Can you decide by yourself how the syntax of the "configuration file" looks like? In this case, you could simply make that file syntactically a Perl program and evaluate it. You would then not have to worry about parsing.

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-07-22 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (112 votes), past polls