http://qs1969.pair.com?node_id=125831
Category: Miscellaneous
Author/Contact Info Hendrik Van Belleghem (beatnik -at- quickndirty -dot- org)
Description: 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. I'll probably have it on CPAN at some point. At the moment I stashed up at perlmonk.org. Examples are included and online.
Update: removed that second my on $line (thanks blakem).
I don't know if it's too obvious but it's not a regular line numbering script. It's a source filter :)))
package Filter::NumberLines;

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

$VERSION = '0.01';

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__
=pod

=head1 NAME

Filter::NumberLines - Source filter for Numbering lines.

=head1 SYNOPSIS

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

  use Filter::NumberLines;

=head1 DESCRIPTION

Filter::NumberLines - Source filter for Numbering lines.

=head1 REQUIREMENTS

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

=head1 TODO

Make number of digits in line number configurable.

=head1 DISCLAIMER

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

=head1 VERSION

This is Filter::NumberLines 0.01.

=head1 AUTHOR

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

=head1 SEE ALSO

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/

=cut
Replies are listed 'Best First'.
Re: Source code line numbering
by blakem (Monsignor) on Nov 17, 2001 at 01:26 UTC
    Hey, thats pretty slick. I am getting a "my" variable $line masks earlier declaration in same scope when warnings are turned on, so you might want to drop the my from the second my $line invocation.

    -Blake

Re: Source code line numbering
by data64 (Chaplain) on Nov 17, 2001 at 07:01 UTC
    Sorry for appearing thick headed, but what does this do that you could not get from cat -n or a good editor (setnu.el mode for emacs and <ESC>:set nu for vi).
    And I do use cat -n on windoze boxes too.
    I do appreciate your effort though.
      Well actually this adds line numbers to your code while still making it runeable. You add that use Filter::NumberLines; at the top, you run your code and the line numbers appear. After that, you still can run the code without actually having to remove the line numbers. Check the examples on the URL mentioned above. If you installed the package, those examples REALLY work, they're not just pretty. BTW if you really want to see some source filters in action, I recommend looking into Acme::Bleach and Acme::Buffy.

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: Source code line numbering
by grinder (Bishop) on Nov 19, 2001 at 21:01 UTC
    Heh.

    Now all you have to do is remap my to LET, substr to MID and # to REM... and we'll be on our way to a halfway decent BASIC interpreter!

    --
    g r i n d e r