OK, same caveat as before - apparent bugs in Perl are usually user
errors, but once again, I'm stumped. Here is my code. The problem I
have is in the second call to getCached(), specifically, the line:
my($nextID,$maxID) = @$L if $L;
The parameter to getCached, $Customer, has a different value than
during the first call. Because of that, the variable $L is undefined.
What I expect the statement above to do is to create 2 new variables,
$nextID and $maxID. Then, if $L has a true value in it, it's treated
as a reference to a list and the first 2 values in that list would be
assigned to the variables. However, if $L is false, then the
variables would be left undefined. What's really blowing my mind is
the that values in $nextID and $maxID get the values THAT THEY WERE
ASSIGNED DURING THE PREVIOUS CALL TO THIS FUNCTION. Wow! They're "my"
variables inside the getCached() function. How could they end up with
values from a previous invocation of the function?
I've added the output from when I run this below. BTW, I'm running
ActiveState ActivePerl v. 5.8.7 Build 815 under MS Windows 2000 Server.
# ---------------------------------------------------
use strict;
our $hCache = {};
my $id1 = getCached('x');
my $id2 = getCached('y');
print("id1 = '$id1\n");
print("id2 = '$id2\n");
# ---------------------------------------------------
sub getCached { my($Customer) = @_;
my $L = $hCache->{$Customer};
print(defined($L) ? "L defined\n":"L undefined\n");
my($nextID,$maxID) = @$L if $L;
print("nextID = '$nextID', maxID = '$maxID'\n");
if ($nextID==$maxID) {
($nextID,$maxID) = getNextMax($Customer);
$hCache->{$Customer} = [$nextID+1, $maxID];
}
else {
++$hCache->{$Customer}->[0];
}
return $nextID;
} # getCached()
# ---------------------------------------------------
sub getNextMax { my($CustomerName) = @_;
if ($CustomerName eq 'x') {
return (100,150);
}
elsif ($CustomerName eq 'y') {
return (200,250);
}
} # getNextMax()
# ---------------------------------------------------
OUTPUT:
# ---------------------------------------------------
C:\Scripts>testIDs.pl
L undefined
nextID = '', maxID = ''
L undefined
nextID = '100', maxID = '150'
id1 = '100
id2 = '100
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs