Hello all, I am probably over my head here, but here goes. I'm trying to pass a hash to a module and it's not working. In my main script, I define a 'module_map' which lists all the modules and calls to them.
sub process_modules { my ($active_modules_ref) = @_; # Accept %active_modules as a refe +rence my %module_map = ( 'clouds' => sub { CloudUpdate::cloud_update() }, 'volcanoes' => sub { VolcanoXML::process_volcano_data() }, 'storms' => sub { Storm::fetch_and_process_storms() }, 'quakes' => sub { Earthquake::get_quakedata() }, 'norad' => sub { my $satellite_file = "$xplanet_satellites_dir\\Norad"; my $output_tle_file = "$xplanet_satellites_dir\\Norad.tle" +; my $marker_file = "$xplanet_satellites_dir\\Norad_marker.t +xt"; Norad::process_satellites($satellite_file, $output_tle_fil +e, $marker_file); }, 'fires' => sub { Fires::run() }, 'labelupdate' => sub { print "process_modules - Debug: Calling WriteoutLabel with + active_modules_ref:\n" if $DEBUG; foreach my $key (keys %$active_modules_ref) { print " $key => $active_modules_ref->{$key}\n" if $DE +BUG; } Label::WriteoutLabel($active_modules_ref); # Pass as a re +ference }, ); foreach my $module (keys %Globals::modules) { my ($onoff_key) = grep { /onoff$/i } keys %{ $Globals::modules +{$module} }; if ($onoff_key && $Globals::modules{$module}{$onoff_key} == 1) + { print "Processing module: $module\n" if $DEBUG; if (exists $module_map{$module}) { $module_map{$module}->($active_modules_ref); } else { warn "No subroutine mapped for module: $module\n" if $ +DEBUG; } } else { print "Module: $module, On/Off: Undefined or Inactive\n" i +f $DEBUG; } } }
The key module in this 'process_modules' subroutine is the labelupdate call
'labelupdate' => sub { print "process_modules - Debug: Calling WriteoutLabel with + active_modules_ref:\n" if $DEBUG; foreach my $key (keys %$active_modules_ref) { print " $key => $active_modules_ref->{$key}\n" if $DE +BUG; } Label::WriteoutLabel($active_modules_ref); # Pass as a re +ference
Given that's the call to use, then my main script invokes process_modules as follow:
# Process modules process_modules(\%active_modules);
Where the program goes off to the Label::WriteoutLabel routine and dies because the hash is undefined. I can't figure out how to pass the hash in. I double checked the process_modules call uses '\%...' so I am stumped. Oh, the WriteoutLabel routine reads as follows:
package Label; use strict; use warnings; use Data::Dumper; use Time::Local; # Load the Time::Local module use Globals qw( $DEBUG $label_file $labelsettings ); sub WriteoutLabel { print "Label line 31 - Debug: \$DEBUG is " . ($DEBUG ? "enabled" : + "disabled") . "\n"; my ($active_modules_ref) = @_; # Skip label generation if labelsdisplay is disabled my $labels_display = $Globals::modules{'labels'}{'labelsonoff'} // + 1; # Default to 1 (enabled) return unless $labels_display; print "Label line 39 - Debug: Labels display is " . ($labels_displ +ay ? "enabled" : "disabled") . "\n" if $DEBUG; # Debug: Check that active_modules_ref is a hash reference unless (ref $active_modules_ref eq 'HASH') { print "Label::WriteoutLabel - Received invalid active_modules_ +ref: " . (ref $active_modules_ref || 'undefined') . "\n" if $DEBUG; die "Error: active_modules_ref is not a HASH reference."; } ...
I have tried my ($self, $active_modules_ref) = @_; and varieties thereon. Thank you in advance,

In reply to Trying to pass Hash to Module by mcoblentz

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.