update: I'm using 'use strict; use warnings' now, but now the issue is that the ProcessFile subroutine, when %do_for is defined, it goes through and executes each subroutine. I'd expected it to only run later, when I run $do_for{$what_to_do} explicitly. I'd like to use this instead of a cascading if... thoughts?
Calling a subroutine is calling a subroutine. Why would you expect it to somehow magically create a subroutine reference and not execute the code?? It's just in list context -- the '=>' is just a special form of the comma that usually lets you get way with using barewords to the left of it. For anything to the right of it there is really no difference between '=>' and ','.

It's similar to what you do later on:

print "here GetHosts - " . Dumper($ref_data) . "\n";

Which can be rewritten with commas like below (which in theory performs better because concatenation can be expensive, especially when it's unnecessary):

print "here GetHosts - ", Dumper($ref_data), "\n";

Now, you would expect Dumper to actually be called here vs. creating a reference to it, right? Right?? So what you need to do is create subroutine references ('\&') like in the following (untested; based on some really old code):

my %do_for = ( get_hosts => \&GetHosts, get_new_fw_rules => \&GetNewFWRules, ); #... $do_for({$what_to_do}->( $_, \%data );

Elda Taluta; Sarks Sark; Ark Arks


In reply to Re: Hash reference undefined somewhere? by Argel
in thread Hash reference undefined somewhere? by bowei_99

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.