Suppose I have a utility library (other
) containing a subroutine
(sort_it
) which I want to use to return arbitrarily sorted data.
It's probably more complicated than this, but this illustrates the
key concepts:
#!/usr/local/bin/perl
use strict;
package other;
sub sort_it {
my($data, $sort_function) = @_;
return([sort $sort_function @$data]);
}
Now let's use it in another package.
package main;
use Data::Dumper;
my($data) = [
{'animal' => 'bird', 'legs' => 2},
{'animal' => 'black widow', 'legs' => 8},
{'animal' => 'dog', 'legs' => 4},
{'animal' => 'grasshopper', 'legs' => 6},
{'animal' => 'human', 'legs' => 2},
{'animal' => 'mosquito', 'legs' => 6},
{'animal' => 'rhino', 'legs' => 4},
{'animal' => 'tarantula', 'legs' => 8},
{'animal' => 'tiger', 'legs' => 4},
],
my($sort_by_legs_then_name) = sub {
return ($a->{'legs'} <=> $b->{'legs'} ||
$a->{'animal'} cmp $b->{'animal'});
};
print Dumper(other::sort_it($data, $sort_by_legs_then_name));
This doesn't work, due to a subtle problem. $a
and $b
are package
globals. They refer to $main::a
and $main::b
when wrapped up in
the closure.
We could fix this by saying, instead:
my($sort_by_legs_then_name) = sub {
return ($other::a->{'legs'} <=> $other::b->{'legs'} ||
$other::a->{'animal'} cmp $other::b->{'animal'});
};
This works, but forces us to hardcode the name of our utility package
everywhere. Were that to change, we'd need to remember to change the
code, not just the use other qw(sort_it);
statement that would likely
be present in the real world.
You might immediately think to try using __PACKAGE__
. That winds
up evaluating to "main". So does eval("__PACKAGE__");
.
There's a trick using caller
that works:
my($sort_by_legs_then_name) = sub {
my($context) = [caller(0)]->[0];
my($a) = eval("\$$context" . "::a");
my($b) = eval("\$$context" . "::b");
return ($a->{'legs'} <=> $b->{'legs'} ||
$a->{'animal'} cmp $b->{'animal'});
};
But this is rather black-magical. It seems like there ought to be some better solution to this. But I haven't found it or figured it out yet.