Don't use bareword filehandles. Instead, use a lexical var for the handle. Also, use the 3 arg form of open and include the reason it failed in the die statement (i.e., include the $! var in the statement).

Don't use '&' when calling a sub unless you know and understand its side effects and want/need those side effects.

Thanks alot ! 1) if you look at the source lines you will see 3 IP's, the first one just right after the date and time (starting from charachter 16). following that IP comes two others which are the real source and destination IP's (which the code fails to fetch via regex), I tried using : if (index ($line,$arguments {"-src"}) < index($line,$arguments {"-dst"})){

but when I print the lines I see a strange phenomenon, if the user use -src 10.(no matter what goes here) , the search pattern gets tackled on charachter 16 - weird ha ?

regarding your other remarks, I will definitely use them, thank you so much for that !

I have 2 questions in regards :

1) "use the 3 arg form of open and include the reason it failed in the die statement (i.e., include the $! var in the statement)."

Will it be alright if you take out the example from my code and show here what you mean ? (I need to see it in order to understand)

2) "Don't use '&' when calling a sub unless you know and understand its side effects and want/need those side effects."

a. my interpreter fails to read the call for the subroutine if I will not use '&'...:/ b. what are the side effects (sorry I am new to perl) ?

I tried that, or something very similar to that, when I wrote my previous message, but it did not seem to work properly. I must have made a silly mistake somewhere when I tested it.

Anyway, what really seemed to puzzle the OP was how to pick up the second and third IP addresses and not the first one, so my examples really focussed on various ways to do that, and were hopefull progressively built for pedagogical purposes.

In Reply To

You made an excellent point: IP regex should be made much more selective...

I fully agree. My very simplistic IP regex was just for the purpose of demonstrating the code that followed, and I said it was too simplistic.

At the very least, I would check for the number of digits in each segment with something like this:

Code

my $ip = qr/(?:\d{1,3}\.){3}\d{1,3}/;

or, better, I would be trying to check at least to a certain extent the ranges of each octet one way or another.

But, then, of course, there may be a mistake in my code above or in whichever code I would try to add for checking the range (and I know for a fact it is not an easy task to get it really right), it is unlikely that I will have tested it nearly as thoroughly as the Regexp::Common module has been tested.

So, yes, agreed, using such a module is usually a better idea.

Having said that, the files I am working on are usually not free format files, I can usually get away with quite simpler validation rules.

But I also have had to try to validate URLs or e-mail addresses (or, I should rather say, I had to to try to detect improper URLs or e-mail addresses), that can easily become a real nightmare.

...my examples really focussed on various ways to do that, and were hopefull progressively built for pedagogical purposes.

Yes, excellent! And these were well done...

...I am working on are usually not free format files, I can usually get away with quite simpler validation rules.

Another good point. In other forums (e.g., StackOverflow), I've seen solutions coded for such a wide range of unlikely exceptions. I can understand wanting to create exception-tolerant code, but if the dataset is consistent, I'm not too sure that extra effort's justified.