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?

I'm having a problem with a hash assignment, and I'm sure it's something trivial, but I'm not seeing it. The indexes and values in the hash are all defined, so I can't see why Perl is complaining.

I have this chunk of code to process different actions:

my %do_for = ( get_hosts => GetHosts($_, \%data), get_new_fw_rules => GetNewFWRules($_, \%data), ); #print "while\n"; if ( ! $do_for{$what_to_do} ) { warn "ERR - what to do $what_to_do"; };

and the GetHosts sub is:

sub GetHosts { my ($line, $ref_data) = @_; my ($hostname, $old_ip, $new_ip) = split /,/, $line; print "here GetHosts - " . Dumper($ref_data) . "\n"; if ($old_ip) { print "here old ip $old_ip, hostname $hostname, new ip $new_ip + \n"; my $ref_data->{$old_ip}{"hostname"} = $hostname; my $ref_data->{$old_ip}{"new_ip"} = $new_ip; } }

and when I run it, I get this:

here GetHosts - $VAR1 = {}; ere old ip a-valid-ip-here, hostname valid-hostname-here, new ip vali +d-ip-here Can't use an undefined value as a HASH reference at ./fw-rule-convert. +pl line 60, <$IN_FH> line 1. Can't use an undefined value as a HASH reference at ./fw-rule-convert. +pl line 65, <$IN_FH> line 1.

But I don't understand - the only undefined value is the hash itself, which I'm trying to add to; I think that should be OK. The values from the split are in fact the correct values in the first line in the input file, and are not null.

Anybody have thoughts as to why I'm getting this error?

-- Burvil


In reply to 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.