cunningrat has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; ... foreach my $date (@dates) { foreach my $line (@{$maint_hash{$address}}) { my (@alljobs,@alljobids,@received); my %rtoa; runsingle($line,$date,\*OUT); } } ... } sub runsingle { ... some code that actually populates the declared variables... printjob($fh,$h,@foo); ... } sub printjob { my $fh=$_[0]; my $h=$_[1]; shift @_; shift @_; foreach my $key (@_) { print $fh $h->open('tr'); my $key2=$rtoa{$key}; print $fh $h->td(${$alljobs[$key2]}[2],${$alljobs[$key2]}[3],${ +$alljobs[$ key2]}[4]); my ($chopdate,$choptime,$junk)= split " ",${$received[$key]}[4] +; $chopdate=~s/-201.//; print $fh $h->td("Ran at $choptime",$chopdate,${$received[$key] +}[3]); print $fh $h->close('tr'); } print $fh $h->close('table'); }
When I run this code, it complains that global symbols "%rtoa" and "@alljobs" require an explicit package name. The complaints are coming from inside the printjob subroutine. The variables are declared with "my" in the inner loop inside the main program, which means that their scope is limited to that inner loop. The ONLY place runsingle is called from is inside that inner loop, and the ONLY place printjob is called from is inside runsingle, so I'd think both printjob and runsingle should see those variables as "global".
Moving the declaration inside the runsingle subroutine doesn't change the behavior.
I don't understand why it's complaining. Would some kind soul explain it to me, please?
I could get around the error by passing those arrays and hashes as parameters... but as you can see from the printjob code, the arrays are actually multidimensional, so the resulting code would be hairy and hard to maintain.
UPDATE: nevermind, got it to work by declaring the variables inside a block that contains both subroutines. It's not elegant, but it'll do.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Global symbol requires explicit package name
by choroba (Cardinal) on Nov 28, 2012 at 14:53 UTC | |
|
Re: Global symbol requires explicit package name
by ColonelPanic (Friar) on Nov 28, 2012 at 15:12 UTC | |
|
Re: Global symbol requires explicit package name
by greengaroo (Hermit) on Nov 28, 2012 at 15:34 UTC | |
by ColonelPanic (Friar) on Nov 28, 2012 at 15:42 UTC | |
by greengaroo (Hermit) on Nov 28, 2012 at 16:01 UTC | |
by ColonelPanic (Friar) on Nov 28, 2012 at 16:43 UTC | |
by greengaroo (Hermit) on Nov 28, 2012 at 18:34 UTC | |
| |
by cunningrat (Acolyte) on Nov 28, 2012 at 17:30 UTC |