public
Last active

  • Download Gist
trick_constant.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#!/usr/bin/perl
 
use warnings;
use strict;
 
use 5.010;
 
use File::Spec::Functions qw(catfile);
 
sub make_inc_hook
{
my %new_vals = @_;
 
my ($path) = grep { -f $_ } map {
catfile( $_, qw/ AnyEvent constants.pl / );
} @INC or die "Couldn't find AnyEvent/constants.pl";
open my $fh, '<', $path or die "open: $!";
 
my $const_match = sprintf 'sub (%s) [(][)] { [^ ]+ }',
join q{|}, keys %new_vals;
 
my $filter = sub {
return 0 unless $_;
s{$const_match}{sub $1 () { $new_vals{$1} }};
return 1;
};
 
return sub {
my ($self, $target) = @_;
return undef unless ( $target eq 'AnyEvent/constants.pl' );
return ( $fh, $filter );
};
}
 
unshift @INC, make_inc_hook( WIN32 => 1,
WSAEINVAL => 1002,
WSAEWOULDBLOCK => 10035,
WSAEINPROGRESS => 10036,
);
 
require "AnyEvent/constants.pl";
say "AnyEvent::WIN32() = ", AnyEvent::WIN32();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.