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

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

Thread Previous
From:
Brad Gilbert
Date:
September 16, 2012 18:27
Subject:
[perl #114934] Fix minor error in tempfile() from t/test.pl
Message ID:
rt-3.6.HEAD-11172-1347831186-163.114934-75-0@perl.org
# New Ticket Created by  "Brad Gilbert" 
# Please include the string:  [perl #114934]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=114934 >


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

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;

    $a++;
    $b = $b + 1;

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

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About