For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > match and grab









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author match and grab
Tim Wolak

2006-08-02, 9:57 pm

Hello all,

I am writing a script to monitor a file and if a line is matched write
it to a file, then grab the IP address from that line and write it to
another file. As I'm ok on working with the files, I need a little help
as to how to get the IP address out of the line of text into a variable
for use. Any help would appreciated.

Tim
Rob Dixon

2006-08-02, 9:57 pm

Tim Wolak wrote:
>
> I am writing a script to monitor a file and if a line is matched write
> it to a file, then grab the IP address from that line and write it to
> another file. As I'm ok on working with the files, I need a little help
> as to how to get the IP address out of the line of text into a variable
> for use. Any help would appreciated.


Hi Tim

In the absence of sample data, I thought the output of Windows' ipconfig would
suffice. I hope the code below helps you do what you want. The regex just
searches for any sequence of four decimal numbers separated by dots and doesn't
check that the string is a valid IP address, but it is likely to suffice in most
cases.

Rob



use strict;
use warnings;

while (<DATA> ) {
if (/(\d+\.\d+\.\d+\.\d+)/) {
print $1, "\n";
}
}

__DATA__

PPP adapter Fast4:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 62.41.132.69
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 62.41.132.69


OUTPUT

62.41.132.69
255.255.255.255
62.41.132.69

Rob Dixon

2006-08-02, 9:57 pm

Tim Wolak wrote:
>
>On Wed, 2006-08-02 at 17:05 +0100, Rob Dixon wrote:
>
[snip data][color=darkred]
>
>Thanks for the example code it worked like a charm. My one question is
>what exactly does the comma do after the print $1,? If you do not
>include it you get an error about an un open filehandle.


Hello Tim.

* Please keep subject threads on the list so that everyone may benefit, and

* Please bottom-post your responses. It makes threads much easier to follow.

The line:

print $1, "\n";

is a function call with the optional parentheses omitted. It is the same as

print($1, "\n");

which also works, but Perl code often has optional syntax elements omitted for a
cleaner look. print() takes a list of things to print as its parameters, so this
line will ouput the value of the $1 variable followed by a newline. The list may
be as long as you like, with as many commas as necessary to separate them.

The print function takes an optional filehandle, when the call would look like

print STDOUT $1, "\n"

and in this format print is really a method call on an IO::Handle object which
has an alternative syntax

STDOUT->print($1, "\n");

(The parentheses are necessary here as print isn't prototyped as a method call.

If you miss out the comma with

print $1 "\n"

then Perl looks at this as a method call on $1 as an IO::Handle object, or

$1->print("\n");

and you get an error message since an IP address string isn't a valid IO::Handle
object!

Take a look at

perldoc -f print

which explains the syntax of the call.

HTH,

Rob
usenet@DavidFilmer.com

2006-08-02, 9:57 pm

Tim Wolak wrote:
> as to how to get the IP address out of the line of text into a variable


That sounds like a very common type of regular expression. So consider
Regexp::Common, which will reliably match lots of different types of
things:

#!/usr/bin/perl
use warnings; use strict;

use Regexp::Common qw{ net };

while ( <DATA> ) {
/($RE{net}{IPv4})/ and print "IP address on line $.: $1\n";
}

__DATA__
Router1: 192.168.1.100.
Nothing to see here.
Server2 is 170.2.11.11 in the data center.

Paul Lalli

2006-08-02, 9:57 pm

Rob Dixon wrote:
> The print function takes an optional filehandle, when the call would look like
>
> print STDOUT $1, "\n"
>
> and in this format print is really a method call on an IO::Handle object which
> has an alternative syntax
>
> STDOUT->print($1, "\n");


Wow. About 8 years of programming Perl, and I never made this
connection. I always thought it was just some syntactical oddity that
the "optional argument" wasn't followed by a semicolon. It makes
perfect sense with what I know about the various syntactic sugar
available for method calls, but the two just never clicked in my mind.
Thank you very much, Rob, for enlightening me.

Paul Lalli

John W. Krahn

2006-08-02, 9:57 pm

Rob Dixon wrote:
>
> If you miss out the comma with
>
> print $1 "\n"
>
> then Perl looks at this as a method call on $1 as an IO::Handle object, or
>
> $1->print("\n");


No.

perldoc -f print
print FILEHANDLE LIST
print LIST
print Prints a string or a list of strings. Returns true if
successful. FILEHANDLE may be a scalar variable name, in which
case the variable contains the name of or a reference to the
filehandle, thus introducing one level of indirection.

If the comma is missing then the first scalar or bareword argument is treated
as a filehandle.



John
--
use Perl;
program
fulfillment
Rob Dixon

2006-08-02, 9:57 pm

John W. Krahn wrote:
>
> Rob Dixon wrote:
>
>
>
> No.
>
> perldoc -f print
> print FILEHANDLE LIST
> print LIST
> print Prints a string or a list of strings. Returns true if
> successful. FILEHANDLE may be a scalar variable name, in which
> case the variable contains the name of or a reference to the
> filehandle, thus introducing one level of indirection.
>
> If the comma is missing then the first scalar or bareword argument is treated
> as a filehandle.


Hmm. Well if I was wrong I'd like to be corrected, but it certainly /looks/ and
/behaves/ like indirect-object method call syntax, and the equivalent arrow
syntax works and does the same thing. But whether it's really a method call
under the hood I'm not absolutely certain, especially when there are oddities
like

print(STDOUT "text\n");

working fine. Whatever it is, it certainly stops being either a list operator or
a function call, so what else can it be?

Rob
John W. Krahn

2006-08-02, 9:57 pm

Rob Dixon wrote:
> John W. Krahn wrote:
>
> Hmm. Well if I was wrong I'd like to be corrected, but it certainly
> /looks/ and
> /behaves/ like indirect-object method call syntax, and the equivalent arrow
> syntax works and does the same thing. But whether it's really a method call
> under the hood I'm not absolutely certain, especially when there are
> oddities like
>
> print(STDOUT "text\n");
>
> working fine. Whatever it is, it certainly stops being either a list
> operator or a function call, so what else can it be?


So you are saying that everytime print() is used IO::Handle gets sucked in
somehow? And how would this have worked in Perl1 through Perl4 before OO was
added?



John
--
use Perl;
program
fulfillment
Rob Dixon

2006-08-02, 9:57 pm

John W. Krahn wrote:
>
> So you are saying that everytime print() is used IO::Handle gets sucked in
> somehow?


Well it's not so far-fetched. After all a lot of magical things happen in Perl,
and PerlIO does an on-demand load thing behind the scenes in 5.8. But no, I know
that print() is a built-in but thought that it was hooked in on the back of a
indirect object method call syntax. A couple of things support this: first that

STDOUT->print()

works fine, and seondly that it's impossible to write a simple subroutine
prototype that does what print() and printf() do whereas that can be done for
all (?) other Perl functions. On the other hand you /can/ write an object method
that behaves the same syntactically.

> And how would this have worked in Perl1 through Perl4 before OO was added?


Now that is a puzzle and I hadn't thought of it. But it strikes me as an
astonishing choice to provide a unique syntax which is invalid for any other
built-in or user-written subroutine for just two built-in functions, and solely
to allow an optional first parameter. It is also a huge coincidence that that
same syntax pops up later on as a valid construct for object method calls and so
makes everything OK.

I am quite happy to believe you are right John, and if that is so then I
apologise to the list for my mistake, but am also left puzzling over what seems
to be a number of enigmas within a language I thought I knew well.

Cheers,

Rob
Rob Dixon

2006-09-28, 6:57 pm

Rob Dixon wrote:
>
> John W. Krahn wrote:
>
> Well it's not so far-fetched. After all a lot of magical things happen in
> Perl, and PerlIO does an on-demand load thing behind the scenes in 5.8. But
> no, I know that print() is a built-in but thought that it was hooked in on
> the back of a indirect object method call syntax. A couple of things support
> this: first that
>
> STDOUT->print()
>
> works fine, and secondly that it's impossible to write a simple subroutine
> prototype that does what print() and printf() do whereas that can be done for
> all (?) other Perl functions. On the other hand you /can/ write an object
> method that behaves the same syntactically.
>
>
> Now that is a puzzle and I hadn't thought of it. But it strikes me as an
> astonishing choice to provide a unique syntax which is invalid for any other
> built-in or user-written subroutine for just two built-in functions, and
> solely to allow an optional first parameter. It is also a huge coincidence
> that that same syntax pops up later on as a valid construct for object method
> calls and so makes everything OK.
>
> I am quite happy to believe you are right John, and if that is so then I
> apologise to the list for my mistake, but am also left puzzling over what
> seems to be a number of enigmas within a language I thought I knew well.


Sorry about the amount of quoting, but this goes back a few ws and I thought
people may need a reminder.

I've just found a reference in the Perl documentation which says that things are
just as I thought they were, thank goodness!

perldoc perlobj

Indirect Object Syntax

The other way to invoke a method is by using the so-called "indirect
object" notation. This syntax was available in Perl 4 long before
objects were introduced, and is still used with filehandles like this:

print STDERR "help!!!\n";


But presumably it wasn't called 'indirect object notation' back then?

Rob
Tom Phoenix

2006-09-29, 6:57 pm

On 9/28/06, Rob Dixon <rob.dixon@350.com> wrote:

> But presumably it wasn't called 'indirect object notation' back then?


Sure it was. Larry is a linguist, and he saw the filehandle as being
the 'indirect object' in a sentence in which print/printf is the verb
and some data are the direct object.

Give Mary the cup
printf STDERR $data;

If you look at those two as if you were a linguist, you'll see their
common structure. Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com