develooper Front page | perl.perl5.porters | Postings from September 2012

[perl #114934] Fix minor error in tempfile() from t/

Thread Previous
Brad Gilbert
September 16, 2012 18:27
[perl #114934] Fix minor error in tempfile() from t/
Message ID:
# New Ticket Created by  "Brad Gilbert" 
# Please include the string:  [perl #114934]
# in the subject line of all future correspondence about this issue. 
# <URL: >

I noticed some slightly wrong code in tempfile() from t/

It isn't wrong enough that it is necessary to fix, but I went ahead
and fixed it anyway.


The following code:

	my $temp = $count;

	my $try = "tmp$$";
	do {
	    $try = $try . $letters[$temp % 26];
	    $temp = int ($temp / 26);
	} while $temp;

should be something like the following:

	my $temp = $count;

	my $try = "tmp$$";
	do {
	    $try = $try . $letters[$temp % 26];
	    $temp = int ($temp / 26) - 1; # <---
	} while $temp >= 0; # <---

I didn't like how that works, and that it isn't tested.

I decided to break that out into another subroutine, and thoroughly
test it. ( num_to_alpha() )

I also noticed that it had to loop through all filenames that it
already gave out before giving a new one.
So I moved the counter variable to outside of the sub.

I am assuming that $$ always gives the same number in a given test file.
Even if this presumption isn't correct, it should still work correctly.

I included a file to test the proper operation of tempfile().
It doesn't (yet) check to make sure the files are properly removed.
( I assume it will need to use run_perl() or similar for that )


I could have put an underscore in front of num_to_alpha() to denote
it's internal nature.
It might be useful for some tests though:

    $a = 'A';
    $b = 0;

    $b = $b + 1;

    is( $a, num_to_alpha($b) );

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About