1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# Perl package for reading NS trace files
package NS::TraceFileReader;
use 5.005;
use strict;
use warnings;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
BEGIN {
use IO::Handle;
use IO::File;
use NS::TraceFileEvent;
use Exporter ();
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = qw();
%EXPORT_TAGS = ();
@EXPORT_OK = qw();
}
# the constructors:
sub new {
my $self = shift;
my $class = ref($self) || $self;
my $hash = {};
if (@_) {
$hash->{filehandle} = IO::File->new(shift,"r");
} else {
# default to STDIN
$hash->{filehandle} = IO::Handle->new_from_fd(fileno(STDIN),
"r");
}
return bless $hash, $class;
}
sub new_from_fd {
my $self = shift;
my $class = ref($self) || $self;
my $hash = {};
if (@_) {
$hash->{filehandle} = IO::Handle->new_from_fd(fileno(shift),
"r");
} else {
print STDERR "Must supply filehandle to NS::TraceFileReader->new_from_fd(\$filehandle).\n";
}
}
# read and parse a line from the file
sub get_event {
my $self = shift;
my $line = $self->{filehandle}->getline;
if (defined $line) {
return new NS::TraceFileEvent $line;
} else {
return undef;
}
}
# a Perl module must return a true value
1;
|