Hi ccrash,

Here's the code. But I don't know what it means.
my ($srcip) = $whole_event_string =~ /^(\d+\.\d+\.\d+\.\d+)/;
I only understand that it is checking whether the entry would have something like IP Address as above. But does it pass the IP address to the $srcip variables ?

Yes, it does. Your code is using a regular expression to find a pattern that looks like an IP Address. Before telling you how it does it, I recommend you to have a look at the perl documentation on regular expressions. In your particular case, the variable $whole_event_string holds the Log entries. Every Log entry is analysed to see whether it has a pattern that begins (that is the meaning of the ^ symbol) with the following sequence of characters:

\d+ one or more digits \. a dot \d+ one or more digits \. a dot \d+ one or more digits \. a dot \d+ one or more digits

if there is a match, that sequence of characters is assigned to the variable $srcip

The following code illustrates what I just described:

#!/usr/bin/perl use strict; use warnings; while (defined (my $whole_event_string = <DATA>)) { my ($srcip) = $whole_event_string =~ /^(\d+\.\d+\.\d+\.\d+)/; print "\$srcip = $srcip\n"; } __DATA__ 1.2.3.4 - Unauth [09/Oct/2003: 10:12:06 -0700] "GET / HTTP/1.1" 200 19 +79 2.3.4.5 - Unauth [09/Oct/2004: 11:12:06 -0700] "GET / HTTP/1.1" 200 19 +79 3.4.5.6 - Unauth [09/Oct/2005: 12:12:06 -0700] "GET / HTTP/1.1" 200 19 +79 4.5.6.7 - Unauth [09/Oct/2006: 13:12:06 -0700] "GET / HTTP/1.1" 200 19 +79

If you try it, the output should be:

$srcip = 1.2.3.4

$srcip = 2.3.4.5

$srcip = 3.4.5.6

$srcip = 4.5.6.7

I hope this helps

lin0

In reply to Re: Need advice on PERL by lin0
in thread Need advice on PERL by ccrash

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.