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


In reply to Getting a hash from a file by jedikaiti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.