Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Consider the following program that intends to decide if a day is a working day:
#!/usr/bin/perl -l use strict; use warnings; my $dow = 2; while ($dow < 8) { my $workday = 0 if not $dow == 6 and not $dow == 7; $workday = 1 if $dow == 6; ### print "$dow $workday"; $dow++; }
The logic is simple: weekends are not workdays, unless it's a special Saturday marked by the government as an extra workday (line marked by ###, condition simplified for sake of minimal example).
I get the following output:
2 0 3 0 4 0 5 0 6 1 7 1
To use a technical expression, what the hell is going on here? It looks as if the value of 1 somehow "leaked through" to the next iteration of the while loop (however stupid this sounds). Is this a bug? Is it a known bug?
I get this with Perl 5.20.3 (Fedora 22's default 64 bit system Perl)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Paranormal leakage of previous value from conditionally set lexical variable
by Anonymous Monk on Mar 09, 2016 at 19:42 UTC | |
by Anonymous Monk on Mar 09, 2016 at 19:53 UTC | |
by stevieb (Canon) on Mar 09, 2016 at 19:58 UTC | |
Re: Paranormal leakage of previous value from conditionally set lexical variable
by toolic (Bishop) on Mar 09, 2016 at 19:25 UTC | |
by GotToBTru (Prior) on Mar 10, 2016 at 13:31 UTC | |
Re: Paranormal leakage of previous value from conditionally set lexical variable
by 1nickt (Canon) on Mar 09, 2016 at 19:25 UTC |