Filter::NumberLines - Source filter for Numbering lines.


Filter-NumberLines documentation Contained in the Filter-NumberLines distribution.

Index


Code Index:

NAME

Top

Filter::NumberLines - Source filter for Numbering lines.

SYNOPSIS

Top

Just put use Filter::NumberLines; at the top of your source file (below the shebang). It will automagically number your lines starting from the line after the use statement.

  use Filter::NumberLines;

DESCRIPTION

Top

Filter::NumberLines - Source filter for Numbering lines.

NOTE

Top

This module is used in the Source Filters in Perl talk I gave at YAPC::Eu 2.00.2 in Munich.

REQUIREMENTS

Top

Filter::NumberLines requires Filter::Util::Call.

TODO

Top

Make number of digits in line number configurable.

DISCLAIMER

Top

This code is released under GPL (GNU Public License). More information can be found on http://www.gnu.org/copyleft/gpl.html

VERSION

Top

This is Filter::NumberLines 0.02.

AUTHOR

Top

Hendrik Van Belleghem (beatnik -at- quickndirty -dot- org)

SEE ALSO

Top

GNU & GPL - http://www.gnu.org/copyleft/gpl.html

Filter::Util::Call - http://search.cpan.org/search?dist=Filter

Paul Marquess' article on Source Filters - http://www.samag.com/documents/s=1287/sam03030004/


Filter-NumberLines documentation Contained in the Filter-NumberLines distribution.

package Filter::NumberLines;

use strict;
use vars qw($VERSION);
use Filter::Util::Call ;

$VERSION = '0.02';

my $line = 0;

sub import {
my ($type) = shift @_;
my ($ref) = [] ;
filter_add(bless $ref) ;
}

sub filter {
my ($self) = @_ ;
my ($status) ;
if (($status = filter_read()) > 0)
{ s/^\d+\:\t//; }
$status ;
}

open(F,"<$0") || die $!;
open(OUTFILE,">$0.bak") || die $!;
$line = 0;
my $no_go = 0;
my $past_use = 0;
$|++;
while(<F>)
{ $line++;
  if ($past_use && /^\d+\:\t/) { $no_go++;last; }
  if ($past_use)
  { $_ = sprintf ("%03d",$line).":\t".$_; }
  if (/use Filter\:\:NumberLines;/)
  { $past_use++; }
  print OUTFILE $_;
}
close(OUTFILE);
if (!$no_go)
{ unlink($0) || die $!;
  rename ("$0.bak",$0);
  close(F);
  exit;
} else { unlink("$0.bak") || die $!; }
1;
__END__