PerlIO-via-LineNumber

 view release on metacpan or  search on metacpan

lib/PerlIO/via/LineNumber.pm  view on Meta::CPAN

#      2 new value for default increment and default line number
# OUT: 1 current default increment

sub increment {

    # set new default increment if one specified
    $line= $increment= $_[1] if @_ >1;

    return $increment;
} #increment

#-------------------------------------------------------------------------------
#
# Subroutines for standard Perl features
#
#-------------------------------------------------------------------------------
#  IN: 1 class to bless with
#      2 mode string (ignored)
#      3 file handle of PerlIO layer below (ignored)
# OUT: 1 blessed object

sub PUSHED { 

    return bless {
      line      => $line,
      format    => $format,
      increment => $increment,
    }, $_[0];
} #PUSHED

#-------------------------------------------------------------------------------
#  IN: 1 instantiated object
#      2 handle to read from
# OUT: 1 processed string

sub FILL {

    # prefix line number
    if ( defined( my $line= readline( $_[1] ) ) ) {
        my $number= $_[0]->{line};
        $_[0]->{line} += $_[0]->{increment};
        return sprintf $_[0]->{format}, $number, $line;
    }

    # nothing to do
    return undef;
} #FILL

#-------------------------------------------------------------------------------
#  IN: 1 instantiated object
#      2 buffer to be written
#      3 handle to write to
# OUT: 1 number of bytes written

sub WRITE {

    # local copies of format and increment
    my ( $format, $increment )= @{ $_[0] }{ qw(format increment ) };

    # print all lines with line number, die if print fails
    foreach ( split m#(?<=$/)#, $_[1] ) {
        return -1
          if !print { $_[2] } sprintf( $format, $_[0]->{line}, $_ );
        $_[0]->{line} += $increment;
    }

    return length( $_[1] );
} #WRITE

#-------------------------------------------------------------------------------
#  IN: 1 class for which to import
#      2..N parameters passed with -use-

sub import {
    my ( $class, %param )= @_;

    # store parameters using mutators
    $class->$_( $param{$_} ) foreach keys %param;
} #import

#-------------------------------------------------------------------------------

__END__

=head1 NAME

PerlIO::via::LineNumber - PerlIO layer for prefixing line numbers

=head1 VERSION

This documentation describes version 0.06.

=head1 SYNOPSIS

 use PerlIO::via::LineNumber;
 PerlIO::via::LineNumber->line( 1 );
 PerlIO::via::LineNumber->format( '%4d %s' );
 PerlIO::via::LineNumber->increment( 1 );

 use PerlIO::via::LineNumber line => 1, format => '%4d %s', increment => 1;

 open( my $in,'<:via(LineNumber)','file.ln' )
  or die "Can't open file.ln for reading: $!\n";
 
 open( my $out,'>:via(LineNumber)','file.ln' )
  or die "Can't open file.ln for writing: $!\n";

=head1 DESCRIPTION

This module implements a PerlIO layer that prefixes line numbers on input
B<and> on output.  It is intended as a development tool only, but may have
uses outside of development.

=head1 CLASS METHODS

The following class methods allow you to alter certain characteristics of
the line numbering process.  Ordinarily, you would expect these to be
specified as parameters during the process of opening a file.  Unfortunately,
it is not yet possible to pass parameters with the PerlIO::via module.

Therefore an approach with class methods was chosen.  Class methods that can



( run in 0.571 second using v1.01-cache-2.11-cpan-71847e10f99 )