Skip to content

Instantly share code, notes, and snippets.

@aklaswad
Created July 27, 2010 03:25
Show Gist options
  • Save aklaswad/491673 to your computer and use it in GitHub Desktop.
Save aklaswad/491673 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
## In sort method, generated sub routine can't access to the lexical variables
## of outer scope...
use strict;
use warnings;
sub my_sort ($$) {
my ( $a, $b ) = @_;
my $foo = 1;
my $code = sub {
return $foo;
};
my $res = $code->() || 0;
print STDERR ( $foo == $res ? "Match\n" : "Not Match\n" );
return $a <=> $b;
}
print STDERR "Just call it\n";
my_sort(1,2); # prints "Match"
print STDERR "Called from sort method\n";
my @sorted = sort my_sort (1,2); # but this prints "Not Match"...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment