raybies has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strictly nested sub warnings
by JavaFan (Canon) on Oct 05, 2010 at 13:09 UTC | |
by raybies (Chaplain) on Oct 05, 2010 at 14:33 UTC | |
by ikegami (Patriarch) on Oct 05, 2010 at 15:57 UTC | |
by raybies (Chaplain) on Oct 05, 2010 at 17:55 UTC | |
by chromatic (Archbishop) on Oct 05, 2010 at 18:03 UTC | |
by eyepopslikeamosquito (Archbishop) on Oct 05, 2010 at 19:35 UTC | |
by raybies (Chaplain) on Oct 05, 2010 at 20:48 UTC | |
by Your Mother (Archbishop) on Oct 05, 2010 at 21:08 UTC | |
by JavaFan (Canon) on Oct 05, 2010 at 16:04 UTC | |
by raybies (Chaplain) on Oct 05, 2010 at 17:52 UTC | |
|
Re: Strictly nested sub warnings
by JavaFan (Canon) on Oct 05, 2010 at 13:03 UTC | |
|
Re: Strictly nested sub warnings
by GrandFather (Saint) on Oct 05, 2010 at 22:37 UTC | |
by pobocks (Chaplain) on Oct 06, 2010 at 04:09 UTC | |
by GrandFather (Saint) on Oct 06, 2010 at 04:42 UTC | |
by ikegami (Patriarch) on Oct 06, 2010 at 04:42 UTC | |
by FloydATC (Deacon) on Oct 06, 2010 at 05:04 UTC | |
by raybies (Chaplain) on Oct 06, 2010 at 11:57 UTC | |
by eyepopslikeamosquito (Archbishop) on Oct 06, 2010 at 12:09 UTC | |
by raybies (Chaplain) on Oct 06, 2010 at 12:26 UTC | |
|
Re: Strictly nested sub warnings
by kcott (Archbishop) on Oct 07, 2010 at 06:55 UTC |