Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I wrote a script that works okay, then I decided to add the "use strict" and "use warnings" to it, and all #$%^@?! has broken loose. Took me half the day to fix everything it was complaining about, but in the process I found two bugs in my script (one of which made me wonder why my script was working in the first place... ah, wondrous perl!), so I suppose it's worth the effort.

Though honesty I wish there were a way to tell perl that the outer most variables were okay not to use the package::variable name... because sticking $main::varname in some of my more specific subs, where I know what I'm doing is really tedious.

Anyhoo... I have one warning left...

Here's a very abbreviated example of the problem:

#!/usr/bin/perl use strict; use warnings; use File::Find; sub SymLinkFind($); my %params; $params{recurseDir} = "."; SymLinkFind(\%params); print "Found ", $params{numoLinks}, " Symbolic Links...\n"; sub SymLinkFind($) { my $reparams = shift; my @SymLinks; sub inlineFind { my $namey = $File::Find::name; if (-l) { push @SymLinks, $namey; } } find \&inlineFind, $reparams->{recurseDir}; $reparams->{Links} = \@SymLinks; $reparams->{numoLinks} = scalar(@SymLinks); return; }

The problem is I get a warning that says:

Variable "@SymLinks" will not stay shared at ./test.pl line 21.

I think this is telling me that were I to attempt to call FindSymLinks again, that due to the internal variable being used in the nested subroutine (named inlineFind) that it would behave as though it were (c equivalent to) a static variable, retaining its values from the last time the function was called, rather than creating a fresh new @SymLinks array.

Is this a correct understanding of the warning? And is there a way to get rid of this warning?

I read somewhere that if I were to assign the sub inlineFind to a variable and provide the reference to that sub through the variable assignment, that it would do some kind of magic and fix the issue, but I haven't been able to figure a way to create a perl variable that references the nested sub.

Any ideas from the gurus?

Thanks,

--Ray


In reply to Strictly nested sub warnings by raybies

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 browsing the Monastery: (5)
As of 2024-04-24 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found