Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Relevant code snippets:
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.


In reply to Global symbol requires explicit package name by cunningrat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found