http://www.perlmonks.org?node_id=830419
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");

When I try to run this, it tells me Global symbol "%Verbs" requires explicit package name at test3.pl line 11. And on lines 13 and 17.

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