ifdef
view release on metacpan or search on metacpan
lib/ifdef.pm view on Meta::CPAN
my ( $ref, $filename, $path )= @_;
# return what we know of this module
if ( !ref $ref ) {
$ref =~ s#/#::#;
return $IFDEF{$ref};
}
# check all directories and handlers
foreach (@INC) {
# let that INC handle do it if it is an INC handle and it's not us
if (ref) {
goto &$_ unless $_ eq $ref;
}
# we found the file
elsif ( -f ( $path= "$_/$filename" ) ) {
# create temp file
open( my $in, $path ) or next;
my $out= IO::File->new_tmpfile
or die "Failed to create temporary file for '$path': $!\n";
$filename =~ s#/#::#;
$IFDEF{$filename}= $path;
# process all lines
local $_= \my $foo; # current state of localizing $_ ?
while ( readline $in ) {
&oneline;
print $out $_;
}
close $in;
&reset;
# make sure we start reading from start again
$out->seek( 0, 0 ) or die "Failed to seek: $!\n";
return $out;
}
}
# indicate that the rest should be searched (which will fail)
return;
} );
# satisfy require
1;
#---------------------------------------------------------------------------
# process
#
# Process a string (consisting of many lines)
#
# IN: 1 string to process
# OUT: 1 processed string (in place change if called in void context)
sub process {
# process all lines
my @line= split m#(?<=$/)#, $_[0];
&reset;
local $_= \my $foo;
&oneline foreach @line;
# close of activating section (e.g. when called by "load")
push @line,"}$/" if $ACTIVATING;
# return if not in void context
return join( '', @line ) if defined wantarray;
# change in place
$_[0]= join( '',@line );
return undef;
} #process
#---------------------------------------------------------------------------
# reset
#
# Reset all internal variables to a known state
sub reset { $ACTIVATING= $INPOD= $DEPTH= 0 } #reset
#---------------------------------------------------------------------------
# oneline
#
# Process one line in $_ in place
sub oneline {
# let the world know if we should
print STDERR "<$_" if DIFF;
# it's a pod marker
if ( m#^=(\w+)# ){
# going back to source
if ( $1 eq 'cut' ) {
$_= $ACTIVATING ? "}$/" : $/;
&reset;
}
# beginning potentially special pod section
elsif ( $1 eq 'begin' ) {
if ( m#^=begin\s+([A-Z_0-9]+)\b# ) {
# activating
if ( $ALL or $ENV{$1} ) {
$_= $ACTIVATING ? "}{$/" : "{;$/";
$ACTIVATING= 1;
$INPOD= 0;
}
# not activating now
else {
$_= $ACTIVATING ? "}$/" : $/;
$ACTIVATING= 0;
$INPOD= 1;
}
}
( run in 1.122 second using v1.01-cache-2.11-cpan-71847e10f99 )