| 1 | package Grepper; | ||||||||||
| 2 | |||||||||||
| 3 | # This is an example of how to use the Text::Filter class. | ||||||||||
| 4 | # | ||||||||||
| 5 | # It implements a module that provides a single instance method: grep, | ||||||||||
| 6 | # that performs some kind of grep(1)-style function (how surprising!). | ||||||||||
| 7 | # | ||||||||||
| 8 | # A class method 'grepper' is also provided for easy access to do 'the | ||||||||||
| 9 | # right thing'. | ||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use base qw(Text::Filter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | our @EXPORT = qw(grepper); | |||||||||
| 16 | |||||||||||
| 17 | # Constructor. All is done by the superclass. | ||||||||||
| 18 | |||||||||||
| 19 | # Instance method, just an example. No magic. | ||||||||||
| 20 | 8 | ▶ | sub grep { | ||||||||
|
Subroutine
grep: called CC=3 SCAR=11.0
| |||||||||||
| 21 | 8 | my $self = shift; | |||||||||
| 22 | 8 | my $pat = shift; | |||||||||
| 23 | 8 | my $line; | |||||||||
| 24 | 8 | ▶ | while ( defined ($line = $self->readline) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 31 | ▶ | $self->writeline ($line) if $line =~ $pat; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | } | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | # Class method, for convenience. | ||||||||||
| 30 | # Usage: grepper (<input file>, <output file>, <pattern>); | ||||||||||
| 31 | 1 | ▶ | sub grepper { | ||||||||
|
Subroutine
grepper: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | my ($input, $output, $pat) = @_; | |||||||||
| 33 | |||||||||||
| 34 | # Create a Grepper object. | ||||||||||
| 35 | 1 | my $grepper = new Grepper (input => $input, output => $output); | |||||||||
| 36 | |||||||||||
| 37 | # Call its grep method. | ||||||||||
| 38 | 1 | $grepper->grep ($pat); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 1 | 1; | |||||||||