Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Repeating Code - there has GOT to be a better way!

by BrowserUk (Pope)
on Mar 31, 2010 at 18:29 UTC ( #832116=note: print w/ replies, xml ) Need Help??


in reply to Repeating Code - there has GOT to be a better way!

As others have said, use a hash rather than symbolic references. Here's how:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my %cmds; for my $fname ( <*Cmd.pm> ) { print $fname; my( $name ) = $fname =~ m[(^.+)Cmd.pm]; %{ $cmds{ $name } } = do $fname; } pp \%cmds; __END__ C:\test>832090.pl billCmd.pm fredCmd.pm { bill => { command_1 => { Command_maps => [], _discussion => "\n some text about command_1", build_types => ["FLIGHT"], cmd_id => 250033, constraints => undef, description => "\n shorter text about command_1 +", fixed_pattern => 143, safety_level => "SAFE", target => "command_target", }, command_2 => { Command_maps => [], _discussion => "\n some text about command_2", build_types => ["FLIGHT"], cmd_id => 250014, constraints => undef, description => "\n shorter text about command_2 +", fixed_pattern => 144, safety_level => "SAFE", target => "command_target", }, }, fred => { command_1 => { Command_maps => [], _discussion => "\n some text about command_1", build_types => ["FLIGHT"], cmd_id => 250033, constraints => undef, description => "\n shorter text about command_1 +", fixed_pattern => 143, safety_level => "SAFE", target => "command_target", }, command_2 => { Command_maps => [], _discussion => "\n some text about command_2", build_types => ["FLIGHT"], cmd_id => 250014, constraints => undef, description => "\n shorter text about command_2 +", fixed_pattern => 144, safety_level => "SAFE", target => "command_target", }, }, }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Repeating Code - there has GOT to be a better way!
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2015-07-29 10:30 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 (263 votes), past polls