| 1 | package Filter::NumberLines::Scratch; | ||
| 2 | |||
| 3 | sub scratch { local $_ = pop; s/^\d+\:\t//gm; return $_; } | ||
| 4 | |||
| 5 | sub import { | ||
| 6 | open 0 or print "Can't number '$0'\n" and exit; | ||
| 7 | my $line = 0; my $no_go = 0; my $past_use = 0; my $file; | ||
| 8 | while(<0>) | ||
| 9 | { $line++; | ||
| 10 | if ($past_use && /^\d+\:\t/) { $no_go++; $file = join "",$_,<0>; last; } | ||
| 11 | if ($past_use) { $_ = sprintf ("%03d",$line).":\t".$_; } | ||
| 12 | if (/use Filter\:\:NumberLines::Scratch;/) { $past_use++; } | ||
| 13 | $file .= $_; | ||
| 14 | } | ||
| 15 | |||
| 16 | if ($no_go) | ||
| 17 | { do { eval scratch $file; exit; } } | ||
| 18 | else { open 0, ">$0" or print "Cannot number '$0'\n" and exit; | ||
| 19 | print {0} $file and exit; } | ||
| 20 | } | ||
| 21 | 1; | ||
| 22 | =pod | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Filter::NumberLines::Scratch - Source filter for Numbering lines (written from scratch). | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | Just put use Filter::NumberLines::Scratch; at the top of your source file (below the shebang). | ||
| 31 | It will automagically number your lines starting from the line after the use statement. | ||
| 32 | |||
| 33 | use Filter::NumberLines::Scratch; | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | Filter::NumberLines::Scratch - Source filter for Numbering lines (written from scratch). | ||
| 38 | |||
| 39 | =head1 NOTE | ||
| 40 | |||
| 41 | This module is used in the Source Filters in Perl talk I'm planning for YAPC::Eu 2.00.2 in Munich. | ||
| 42 | |||
| 43 | =head1 REQUIREMENTS | ||
| 44 | |||
| 45 | Filter::NumberLines::Scratch has no dependencies | ||
| 46 | |||
| 47 | =head1 TODO | ||
| 48 | |||
| 49 | Make number of digits in line number configurable. | ||
| 50 | |||
| 51 | =head1 DISCLAIMER | ||
| 52 | |||
| 53 | This code is released under GPL (GNU Public License). More information can be | ||
| 54 | found on http://www.gnu.org/copyleft/gpl.html | ||
| 55 | |||
| 56 | =head1 VERSION | ||
| 57 | |||
| 58 | This is Filter::NumberLines::Scratch 0.02. | ||
| 59 | |||
| 60 | =head1 AUTHOR | ||
| 61 | |||
| 62 | Hendrik Van Belleghem (beatnik -at- quickndirty -dot- org) | ||
| 63 | |||
| 64 | =head1 SEE ALSO | ||
| 65 | |||
| 66 | GNU & GPL - http://www.gnu.org/copyleft/gpl.html | ||
| 67 | |||
| 68 | Filter::Util::Call - http://search.cpan.org/search?dist=Filter | ||
| 69 | |||
| 70 | Paul Marquess' article | ||
| 71 | on Source Filters - http://www.samag.com/documents/s=1287/sam03030004/ | ||
| 72 | |||
| 73 | =cut |