On Feb 16, 2004, at 2:46 PM, george wrote:

I want to parse a long ns log text file, and I've attempted to write some
perl code to do that. I am a newbie, and I haven't managed to get my code
working.

I'll provide some general comments below.


Basically, the ns file contains information about TCP connections and I want
to create a perl object for each connection, and gather some data. The code
that I created is the following:

Is that one object per line/entry? That sounds like a lot to me.


Have you considered making a Log object that stores all the information and provides summery information for it?

Just out of curiosity, why objects, over say a hash?

Please post some sample log data, so we can see what we're talking about here. Help us help you. ;)

package Connection;

sub new {
        my ($class) = @_;
        my $self = {
                _packet_ids=> {},
                _bytes_per_second => [],
                _source_node => undef,
                _destination_node => undef
        };
        bless $self, $class;
        return $self;
}

I like my constructors like this:


sub new {
        my $class = shift;
        my $self = {
                _packet_ids=> {},
                _bytes_per_second => [],
                _source_node => undef,
                _destination_node => undef,
                @_
        };
        return bless $self, $class;
}

Which is the same as you have it, except I can override default parameter choices at object creation. I do realize that you're using the "_ Means Don't Touch" hint, so this may not be helpful in your case. I'm pretty lax with object "security". I prefer functionality and setting parameters in a constructor just makes sense to me.

sub addToBytesPerSecond {
my ( $self, $currentBytesPerSecond) = @_;
my $atLocation = $self->{_seconds_counter};
$self->{_bytes_per_second}[$atLocation] = $currentBytesPerSecond;
print "";
}

I think we can simplify that a little. You just want to push an entry onto that array, right?


sub addToBytesPerSecond {
        my $self = shift;
        push @{ $self->{_bytes_per_second} }, @_;
}

How's that? You can even add a whole list at once now.

sub printBytesPerSecond {
        my ($self) = @_;
        my $loopCounter = 0;
        my $length = scalar($self->{_bytes_per_second});

        while ($loopCounter < $length) {
                print "$self->{bytes_per_second}[$loopCounter]\n";
                $loopCounter++;
        }
}

Good rule of Perl thumb: If you're typing that much for something simple, like an output loop, you're probably taking the hard road. Easy things are easy in Perl.


sub printBytesPerSecond {
        my $self = shift;
        print "$_\n" foreach @{ $self->{_bytes_per_second} };
}

sub sourceNode {
        my ( $self, $sourceNode ) = @_;
        $self->{_source_node} = $sourceNode if defined($sourceNode);
        return $self->{_source_node};
}

Using objects for everything... names like "sourceNode"... You a Java guy? :D


Perl style for subroutine names is generally "source_node". We think it reads a little better and is easier on our eyes. Just FYi. Use whatever you like, of course.

Us Perl guys are also pretty allergic to extra variables we don't need. We're weird that way. He's how we generally write the above:

sub source_node {
        my $self = shift;
        $self->{_source_node} = shift if @_;
        return $self->{_source_node};
}

sub destinationNode {
my ( $self, $destinationNode) = @_;
$self->{_destination_node} = $destinationNode if defined($destinationNode);
return $self->{_destination_node};
}

I'll let you trim that one, if you like.


The sourceNode and destinationNode functions work fine. But the
printBytesPerSecond and the addToBytesPerSecond functions do not work.
I can't understand what's wrong with them. Moreover, as you can see I also
want to have a hashtable with the packetIds that have been seen while
parsing, so as to filter duplicate packets. I didn't even attempt that,
because I couldn't understand how to insert a scalar into the hash, and how
to retrieve the value.

See if that gets you going. If not, come back with sample data and usage code and we'll get you straightened out.


Good luck.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to