C
cartercc
I have a big glob of data that I'm extracting from a database using
selectall_hashref(). I'm using the primary key to manipulate the data
but I have to print out the data sorted by date, not the primary key.
Here's the relevant portion of the code:
sub output
(
....
foreach my $key (sort bydate keys %{$calendarhash})
{
....
print qq($calendarhash->{$key}{'calid'} $calendarhash->{$key}
{'event'} $calendarhash->{$key}{'eventdate'} $calendarhash->{$key}
{'addby'} $calendarhash->{$key}{'adddate'} \n);
....
}
#----------bydate------------------
sub bydate
{
my $date_a = $calendarhash->{$a}{'eventdate'};
my $date_b = $calendarhash->{$b}{'eventdate'};
return $date_a cmp $date_b;
}
#---------------------------------
}
Here's the error message using diagnostics:
$ perl -cw console
Variable "$calendarhash" will not stay shared at CONSOLE.pm line 191
(#1)
(W closure) An inner (nested) named subroutine is referencing a
lexical variable defined in an outer subroutine.
When the inner subroutine is called, it will probably see the
value of
the outer subroutine's variable as it was before and during the
*first*
call to the outer subroutine; in this case, after the first call
to the
outer subroutine is complete, the inner and outer subroutines will
no
longer share a common value for the variable. In other words, the
variable will no longer be shared.
Furthermore, if the outer subroutine is anonymous and references a
lexical variable outside itself, then the outer and inner
subroutines
will never share the given variable.
This problem can usually be solved by making the inner subroutine
anonymous, using the sub {} syntax. When inner anonymous subs
that
reference variables in outer subroutines are called or referenced,
they
are automatically rebound to the current values of such variables.
[Mon Mar 2 13:58:38 2009] CONSOLE.pm: Variable "$calendarhash" will
not stay shared at CONSOLE.pm line 191.
Variable "$calendarhash" will not stay shared at MUPD.pm line 209 (#1)
[Mon Mar 2 13:58:38 2009] MUPD.pm: Variable "$calendarhash" will not
stay shared at MUPD.pm line 209.
console syntax OK
$
I want to place the bydate() function WITHIN the lexical scope of the
display function, and can't understand why Perl complains. (Actually,
I can understand why Perl complains, I just can't understand why it
SHOULD complain.)
Is this error message significant? If so, should I remove the bydate()
function from the scope of the display function? I don't care to use
an anonymous function as I feel that it needlessly obscures the code,
and I want to keep the sorting function in the main function for the
same reason.
Thanks, CC.
selectall_hashref(). I'm using the primary key to manipulate the data
but I have to print out the data sorted by date, not the primary key.
Here's the relevant portion of the code:
sub output
(
....
foreach my $key (sort bydate keys %{$calendarhash})
{
....
print qq($calendarhash->{$key}{'calid'} $calendarhash->{$key}
{'event'} $calendarhash->{$key}{'eventdate'} $calendarhash->{$key}
{'addby'} $calendarhash->{$key}{'adddate'} \n);
....
}
#----------bydate------------------
sub bydate
{
my $date_a = $calendarhash->{$a}{'eventdate'};
my $date_b = $calendarhash->{$b}{'eventdate'};
return $date_a cmp $date_b;
}
#---------------------------------
}
Here's the error message using diagnostics:
$ perl -cw console
Variable "$calendarhash" will not stay shared at CONSOLE.pm line 191
(#1)
(W closure) An inner (nested) named subroutine is referencing a
lexical variable defined in an outer subroutine.
When the inner subroutine is called, it will probably see the
value of
the outer subroutine's variable as it was before and during the
*first*
call to the outer subroutine; in this case, after the first call
to the
outer subroutine is complete, the inner and outer subroutines will
no
longer share a common value for the variable. In other words, the
variable will no longer be shared.
Furthermore, if the outer subroutine is anonymous and references a
lexical variable outside itself, then the outer and inner
subroutines
will never share the given variable.
This problem can usually be solved by making the inner subroutine
anonymous, using the sub {} syntax. When inner anonymous subs
that
reference variables in outer subroutines are called or referenced,
they
are automatically rebound to the current values of such variables.
[Mon Mar 2 13:58:38 2009] CONSOLE.pm: Variable "$calendarhash" will
not stay shared at CONSOLE.pm line 191.
Variable "$calendarhash" will not stay shared at MUPD.pm line 209 (#1)
[Mon Mar 2 13:58:38 2009] MUPD.pm: Variable "$calendarhash" will not
stay shared at MUPD.pm line 209.
console syntax OK
$
I want to place the bydate() function WITHIN the lexical scope of the
display function, and can't understand why Perl complains. (Actually,
I can understand why Perl complains, I just can't understand why it
SHOULD complain.)
Is this error message significant? If so, should I remove the bydate()
function from the scope of the display function? I don't care to use
an anonymous function as I feel that it needlessly obscures the code,
and I want to keep the sorting function in the main function for the
same reason.
Thanks, CC.