Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Best Way to Skip out of a sub Entirely

by dru145 (Friar)
on Nov 13, 2002 at 16:44 UTC ( [id://212625]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I'm in need of you assistance (once again). I have a script that goes through a firewall log file and extracts certain entries. Well, I'm trying to make this script "smarter" by ignoring entries that I dont' care about. For example, I don't care about any entries that are flagged by what is referred to as rule 0. Here is my subroutine that is doing this as well as collecting the "evidence":
###################################################### # evidence: Open up the log file, search for # the ip, add to array, split array into 15 # lines, test if array is empty or not. ##################################################### sub evidence { my ($count1, $action, $src); foreach (@data){ ($action,$src) = (split /;/)[5,10]; next if m/\b0\b/; #skip any rule 0 matches next if m/^\s*$/; #skip any empty lines if ($action eq 'drop' && $src =~ /$ip/){ push (@fwlog, $_); $count1++ if $src =~ /$ip/; last if $count1 >= 16; } } # Test if the fwlog array is empty if (@fwlog) { } else { return; } }
That is working well, but the problem is, I'm only exiting out of the evidence sub, and the rest of the script continues to execute. I would like for the script to compeletly skip this ip address entirely and move onto the next one. I was thinking a LABEL might work, but this is not being executed in a while loop. Here is the rest of the pertaint parts of the script (note, these subs are called before the evidence sub):
# Run the script against each ip address foreach my $x (0 .. $#ips){ &check($ips[$x][0], $ips[$x][1]); } &check(); my (@data, @fwlog, $ip, $times, $result); ###################################################################### # check: See if the traffic is harmless. This is done by checking if # the source ip remains constant and the service remains farily # constant ###################################################################### sub check { $ip = $_[0]; $times = $_[1]; my ($rule, $dst, $service, @service, @dst, $count); open (OUTFILE, $outfile) or die "Can't open $outfile: $!"; while (<OUTFILE>){ push (@data, $_) if $_ =~ /$ip/; } close OUTFILE; foreach (@data){ ($dst, $service) = (split /;/)[11,12]; next if m/^\s*$/; #skip any empty lines next if $rule =~ m/\b0\b/; #skip any rule 0 matches push(@service, $service); push(@dst, $dst); } @service = &duplicates(@service); @dst = &duplicates(@dst); foreach (@data){ $count++ if /\;$dst[0]\;/ && /\b$service[0]\b/; } &evidence(); if ($count == 0){ next; } elsif ($count >= 75){ &misconfig(); } else { &whois(); } }
I appreciate any suggestions.

Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Best Way to Skip out of a sub Entirely
by Abigail-II (Bishop) on Nov 13, 2002 at 16:49 UTC
    Use return values. For instance, return 1 if you are returning "early" from the evidence function, and whenever you call the evidence function, check the error code. You can propagate this up the call stack.

    Alternatively, use exceptions. die in the evidence sub if you want to bail out, and use eval where you want to continue.

    Abigail

Re: Best Way to Skip out of a sub Entirely
by broquaint (Abbot) on Nov 13, 2002 at 17:01 UTC
    As Abigail-II suggests, use return.

    But just for completeness1 you can use next to skip in your loop e.g

    sub foo { bar(); print "at $_\n" } sub bar { next LOOP if $_ % 2 == 0 } LOOP: for(0 .. 10) { foo() } __output__ at 1 at 3 at 5 at 7 at 9

    HTH

    _________
    broquaint

    1 forgive me Larry, for I am about to sin ...

Re: Best Way to Skip out of a sub Entirely
by nedals (Deacon) on Nov 13, 2002 at 19:24 UTC
    Here's another idea. In your &evidence sub, delete the last test and: return @fwlog;

    Then in your &check sub: unless (&evidence) { return; }

    That should take you back into your FOR loop

      Thanks that worked!

      Dru
      Another satisfied monk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://212625]
Approved by krujos
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found