jmaya has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, i am in desperate need of help to get rid of certain warnings, i tryied taking away the warnings, and strict stubs but no go!!!!!
here is my code

#!/usr/bin/perl use strict; use warnings; my $program = 'netstat -a'; my @lines; my ($protocol, $localAddress, $foreignAddress, $state); open NETSTAT, "$program|" or die "Cannot open netstat $!\n"; @lines = <NETSTAT>; close NETSTAT; foreach (@lines) { /(\w+)\s+(\w+:\w+)\s+(\d+\.\d+\.\d+\.\d+):\w+\s+(\w+)/; if($1, $2, $3, $4) { $protocol = $1; $localAddress = $2; $foreignAddress = $3; $state = $4; print "$protocol, $localAddress, $foreignAddress, $state\n"; } }
------------ end code -------------
results
-----------------------------------
Useless use of a variable in void context at netstatReader.pl line 2 +6. Useless use of a variable in void context at netstatReader.pl line 2 +6.

20030818 Edit by Corion: Fixed formatting

Replies are listed 'Best First'.
Re: Help with warnings!!!
by liz (Monsignor) on Aug 18, 2003 at 20:02 UTC
    What do you think you're accomplishing with:
    if($1, $2, $3, $4) {
    ? I think you probably mean either:
    if($1 or $2 or $3 or $4) { # any of $1..$4 is not empty
    or:
    if($1 and $2 and $3 and $4) { # all of $1..$4 are not empty

    Liz

Re: Help with warnings!!!
by Thelonius (Priest) on Aug 18, 2003 at 20:25 UTC
    You actually want:
    foreach (@lines) { if(/(\w+)\s+(\w+:\w+)\s+(\d+\.\d+\.\d+\.\d+):\w+\s+(\w+)/) { $protocol = $1; $localAddress = $2; $foreignAddress = $3; $state = $4; print "$protocol, $localAddress, $foreignAddress, $state\n"; } }
    This is because if a line fails to match, perl will not reset $1, $2, $3, $4, so they will still have a value from the previous match.

    Also, if you are not going to use the @lines array for something else, it's much more memory-efficient to combine the loops like this:

    open NETSTAT, "$program|" or die "Cannot open netstat $!\n"; while (<NETSTAT>) { if(/(\w+)\s+(\w+:\w+)\s+(\d+\.\d+\.\d+\.\d+):\w+\s+(\w+)/) { print "$1, $2, $3, $4\n"; # protocol, localAddress, foreignAddress, state } }
Re: Help with warnings!!!
by dragonchild (Archbishop) on Aug 18, 2003 at 20:02 UTC
    if($1, $2, $3, $4) ----- if ($1 && $2 && $3 && $4)
    Make that change and it'll be fine. You weren't doing what you thought you were doing. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Help with warnings!!!
by tcf22 (Priest) on Aug 18, 2003 at 20:04 UTC
      Yup!!! thank you guys.... Newbie stuff. slowly but sureley...