jedikaiti has asked for the wisdom of the Perl Monks concerning the following question:
I asked this in CB yesterday, but need further input...
I have a hash in a file. Literally, the file is a few lines of comments followed by...
%Verbs = ( "arm" => { description => " A prerequisite command for a potentially hazar +dous command. ", discrete => 1, }, "close" => { description => " Mechanically close a device controlled by the +command element. \\emph{close} works in conjunction w +ith the \\emph{open} verb. ", discrete => 1, }, "disable" => { description => " Deactivate a capability or function. \\emph{d +isable} works in conjunction with the \\emph{enable} v +erb. ", discrete => 1, }, "dump" => { description => " Copy a specific data buffer of the command ele +ment to telemetry. ", discrete => 0, }, "enable" => { description => " Activate a capability or function. \\emph{ena +ble} works in conjunction with the \\emph{disable} +verb. ", discrete => 1, }, );
All I want to do is call this somehow from my perl script, so that I can use the hash. I have no control over the creation of this file.
bart made the suggestion "Then look at do and require (and eval and use to get the whole picture)", which I did. I looked them up on perldoc.perl.org, and I'm not sure if they don't do what I want them to do, or if I'm just using them wrong. For example:
#!/usr/bin/perl -w use strict; use Template; do 'cmd_verbs_test.pm'; print ("Hello!\n"); #do 'cmd_verbs_test.pm'; # opens file with Verbs hash foreach my $cmd (sort keys %Verbs){ # sets value to description text without preceeding or ending newl +ines and spaces my $desc = $Verbs{$cmd}{description}; print("Command: $cmd \n"); print("Description: $desc \n"); print("Discrete: $Verbs{$cmd}{discrete} \n\n"); } print("Done!\n");
I have also read through Reading Variables from a File and Object Serialization Basics. From reading those, I'm thinking I will need to use one of the modules listed to do what I want to do.
I do, however, want to keep this as simple as possible, and thought I'd ask here to make sure I'm not just doing something wrong with how I'm using do, or if there's another way.
Thanks, Monks!
Kaiti
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting a hash from a file
by BrowserUk (Patriarch) on Mar 24, 2010 at 01:35 UTC | |
|
Re: Getting a hash from a file
by toolic (Bishop) on Mar 24, 2010 at 00:39 UTC | |
|
Re: Getting a hash from a file
by kikuchiyo (Hermit) on Mar 24, 2010 at 10:39 UTC | |
|
Re: Getting a hash from a file
by itsscott (Sexton) on Mar 24, 2010 at 00:01 UTC | |
by jedikaiti (Hermit) on Mar 24, 2010 at 00:08 UTC | |
by itsscott (Sexton) on Mar 24, 2010 at 00:35 UTC | |
by jedikaiti (Hermit) on Mar 24, 2010 at 00:41 UTC | |
by jethro (Monsignor) on Mar 24, 2010 at 01:52 UTC |