mcoblentz has asked for the wisdom of the Perl Monks concerning the following question:
The key module in this 'process_modules' subroutine is the labelupdate callsub 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; } } }
Given that's the call to use, then my main script invokes process_modules as follow:'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
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:# Process modules process_modules(\%active_modules);
I have tried my ($self, $active_modules_ref) = @_; and varieties thereon. Thank you in advance,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."; } ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to pass Hash to Module
by GrandFather (Saint) on Jan 29, 2025 at 02:27 UTC | |
by mcoblentz (Scribe) on Jan 30, 2025 at 23:32 UTC | |
|
Re: Trying to pass Hash to Module
by LanX (Saint) on Jan 29, 2025 at 06:13 UTC | |
|
Re: Trying to pass Hash to Module
by Marshall (Canon) on Jan 29, 2025 at 10:46 UTC |