| Store | Cart
Lists » pdk

Re: perlapp - combine related executables to reduce size

From: Sat Guru Khalsa <satg...@bellatlantic.net>
Sun, 29 Apr 2012 01:05:27 -0400
Thanks to Martin Thurn, $Bill Luebkert, Bill Walters, and Ingo Schwarze  
for the same answer with various explanations and clarifications.

For the benefit of others who may have made the same mistake ... the  
answer is

Don't assign a value to a lexical variable that you are going to use as a  
file or dir handle.

Don't do this:
    my $DIR = "DIR1"; #  Don't assign a value
    opendir ($DIR, ".");

Do it this way
    my $DIR; # just declare it
    opendir ($DIR, ".");

The problem, to which this answer applies, is as follows.

I always use named dir handles as in
     opendir (DIR, ".")

But this did not work for recursion

    sub recurseDir () {
       my ($dir) = @_;
       opendir (DIR, $dir);
       while (defined (my $f=readder(DIR))) {
          if (-d "$dir\\$f") { &recurseDir("$dir\\$f"); }
          .. other wise do something with the plain file ..

On the second time through, opendir on the DIR handle closes the original  
directory.

My solution was to declare $recurseCount globally and increment it in the  
recurseDir sub
       my $DIR = "DIR" . $recurseCount++;
       opendir ($DIR, $dir);
       .. etc ..

This worked fine, but, it forced me to use strict only for "vars",  
otherwise I would get a runtime error
    Can't use string ("DIR1") as a symbol ref while "strict refs" in use

Thanks for pointing me to the right way. So elegantly simple.
The "my" pragma was giving me a new "$DIR" with each recursion,
so there was no need for the recurseCount.

Thanks again.




On Fri, 27 Apr 2012 08:21:23 -0400, Ingo Schwarze  
<Ingo...@sophos.com> wrote:

> Hi,>> Sat Guru Khalsa wrote on Thu, Apr 26, 2012 at 02:15:56PM +0100:>>>     use strict vars;>> instead of>>     use strict;>> Don't disable warning just because your program is buggy;> the point of the warnings is to find the bugs!>>> My code has these lines>>>>     $dir = 'C:\Perl\bin';>>     $DIR = "DIR1";>>     opendir ($DIR, $dir) or die "$!: $dir\n";>> Well, make that>>   my $dirname = 'testdir';>   my $dirhandle;>   open $dirhandle, $dirname or die "$dirname: $!";>   my @files = readdir $dirhandle;>> The open(3p) function assigns to its first argument,> so assigning to it beforehand is pointless and defeats> the purpose.>> Yours,>   Ingo


-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
_______________________________________________
PDK mailing list
P...@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Recent Messages in this Thread
will...@aol.com Mar 17, 2012 09:30 pm
Sat Guru Khalsa Apr 26, 2012 01:15 pm
Sat Guru Khalsa Apr 29, 2012 05:05 am
$Bill Luebkert Apr 29, 2012 01:06 pm
Phillip Richcreek Mar 17, 2012 05:25 pm
Messages in this thread