The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

This documentation describes version 0.04.

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";

DESCRIPTION

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

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 also be called as key-value pairs in the use statement.

Please note that the new value of the class methods that are specified, only apply to the file handles that are opened (or to which the layer is assigned using binmode()) after they have been changed.

line

 use PerlIO::via::LineNumber line => 1;
 
 PerlIO::via::LineNumber->line( 1 );
 my $line= PerlIO::via::LineNumber->line;

The class method "line" returns the initial line number that will be used for adding line numbers. The optional input parameter specifies the initial line number that will be used for any files that are opened in the future. The default is 1.

format

 use PerlIO::via::LineNumber format => '%4d %s';
 
 PerlIO::via::LineNumber->format( '%4d %s' );
 my $format= PerlIO::via::LineNumber->format;

The class method "format" returns the format that will be used for adding line numbers. The optional input parameter specifies the format that will be used for any files that are opened in the future. The default is '%4d %s'.

increment

 use PerlIO::via::LineNumber increment => 1;
 
 PerlIO::via::LineNumber->increment( 1 );
 my $increment= PerlIO::via::LineNumber->increment;

The class method "increment" returns the increment that will be used for adding line numbers. The optional input parameter specifies the increment that will be used for any files that are opened in the future. Setting the increment will also cause the line to be set to the same value. The default is 1.

REQUIRED MODULES

 (none)

EXAMPLES

Here are some examples, some may even be useful.

Write line numbers to a file

The following code creates a file handle that prefixes linenumbers while writing to a file.

 use PerlIO::via::LineNumber;
 open( my $out,'>via(LineNumber)','numbered' ) or die $!;
 print $out <<EOD;
 These lines with
 text will have
 line numbers
 prefixed
 automagically.
 EOD

will end up as

    1 These lines with
    2 text will have
    3 line numbers
    4 prefixed
    5 automagically.

in the file called "numbered".

BASICfy filter

A script that adds linenumbers to a file in good old BASIC style.

 #!/usr/bin/perl
 use PerlIO::via::LineNumber format => '%04d %s', increment => 10;
 binmode( STDIN,':via(LineNumber)' ); # could also be STDOUT
 print while <STDIN>;

would output the following when called upon itself:

 0010 #!/usr/bin/perl
 0020 use PerlIO::via::LineNumber format => '%04d %s', increment => 10;
 0030 binmode( STDIN,':via(LineNumber)' );
 0040 print while <STDIN>;

SEE ALSO

PerlIO::via and any other PerlIO::via modules on CPAN.

COPYRIGHT

Copyright (c) 2002, 2003, 2009, 2012 Elizabeth Mattijsen. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.