Text-BIP

 view release on metacpan or  search on metacpan

lib/Text/BIP.pm  view on Meta::CPAN

	my $self = shift;
	my $ref = shift;
	foreach my $ext ( @_ ) { $self->{__handlers}->{'file'}->{$ext}=$ref; }
}
sub index_handler { 
	my($self,$ref) = @_;
	$self->{__handlers}->{'index'}->{'*'}=$ref;
}
sub read_handler { 
	my $self = shift;
	my $ref = shift;
	foreach my $ext ( @_ ) { $self->{__handlers}->{'read'}->{$ext}=$ref; }
}

sub index { 
	my $self = shift;
	my $path = shift || $self->base() || '.';
	if ($path) {
		$self->{__handlers}->{prerun}->($self) 
			if ( defined($self->{__handlers}->{prerun}) );
		$self->{__index_depth}=1;
		_process_dir($self,$path);
		$self->{__index_depth}=0;
		$self->{__handlers}->{postrun}->($self) 
			if ( defined($self->{__handlers}->{postrun}) );
	} 
}

sub _process_dir {
	my $self = shift;
	my $path = shift;
	my $d = DirHandle->new($path);
	my $exts = $self->{exts};
	for my $file ($d->read()) {
		unless ($file=~/^\./) {
			my $path_file=File::Spec->catfile($path,$file);
			$file=~m/\.([^.]*)$/;
			my $ext = $1 || ''; 
			my %data = ( path=>$path, file=>$file, ext=>$ext );
			push(@{ $self->{__stack} }, \%data );
			if ( -f $path_file) { # all files hook?
				if ( defined( $self->{__handlers}->{file}->{ $ext } ) ) {
					$self->{__handlers}->{'file'}->{ $ext }->($self);
				} elsif ( defined( $self->{__handlers}->{file}->{'*'} ) ) {
					$self->{__handlers}->{'file'}->{'*'}->($self);
				}
			} elsif (-d $path_file) {
				$self->{__handlers}->{'index'}->{'*'}->($self) 
					if ( defined( $self->{__handlers}->{'index'}->{'*'} ) );
				$self->{__index_depth}++;
				_process_dir($self, $path_file) 
					if (! $self->depth || $self->{__index_depth} < $self->depth);
				$self->{__index_depth}--;
			}
			pop(@{ $self->{__stack} });
		}
	}
	1;
}

# accessors methods to current state while streaming.
sub dir { $_[0]->{__stack}->[-1]->{path} || $path_delim; }
sub relative_dir { 
	my $base = $_[1] || $_[0]->base; 
	$_[0]->{__stack}->[-1]->{path}=~m/^$base(.*)/ ? $1 : '';
}
sub file { $_[0]->{__stack}->[-1]->{file} || ''; }
sub ext { $_[0]->{__stack}->[-1]->{ext} || ''; }
sub name { 
	my $x=$_[0]->{__stack}->[-1]; 
	File::Spec->catfile( $x->{path}, $x->{file} );
}
sub relative_name {
	my $base = $_[1] || $_[0]->base; 
	$_[0]->name=~/^$base(.*)/;
	$1;
}
sub index_depth { $_[0]->{__index_depth}; }

# experimental convienence method for reading files automatically based on type.
# should this die silently or should a default handler be implemented?
sub read_file {
	my $self = shift;
	my $file = shift;
	(my $ext) = $file=~/\.([^.]*)$/;
	if ( my $hdlr = $self->{__handlers}->{'read'}->{$ext} ) {
		return $hdlr->($self, $file, @_);
	} elsif ( my $hdlr = $self->{__handlers}->{'read'}->{'*'} ) {
		return $hdlr->($self, $file, @_);
	} else { 
		warn "Undefined handler for file extension: $ext"; 
		return '';
	}
} 

1;

__END__

=head1 NAME

Text::BIP (Blosxom Infrastructure Package) -- an object-oriented module for facilitating 
event-based file system indexing.

=head1 SYNOPSIS

 #!/usr/bin/perl -w
 
 use Text::BIP;

 # create object and initialize
 my $bip = new Text::BIP;
 $bip->depth(1); # do no index subdirectories. default is 0 recurse through all. 
 $bip->base('/some/path/name');
 
 # ... or initialize in the constructor.
 my $bip = Text::BIP->new( { depth=>1, base=>'/some/path/name' } );

 # set a file handler for .txt files.
 $bip->file_handler(\&hdlr_file,'txt');
 $bip->index_handler(\&hdlr_index);



( run in 1.191 second using v1.01-cache-2.11-cpan-39bf76dae61 )