Tim Wolak wrote:
>
>On Wed, 2006-08-02 at 17:05 +0100, Rob Dixon wrote:
>
>>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.
>>
>>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.
>>
>>
>>
>>use strict;
>>use warnings;
>>
>>while (<DATA>) {
>>   if (/(\d+\.\d+\.\d+\.\d+)/) {
>>     print $1, "\n";
>>   }
>>}
[snip data]
>
>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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to