[ Please do not top-post.  Please remove any quoted text that is not relevant
to your post. ]


Angus wrote:
> From: Hans Meier (John Doe)
>> 
>> Here is a way to process one lease { } 
>> after another, with the possibility to extract every field you want.
>> 
>> I think it is easy to read, understand, and alter.
>> 
>> =====
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>> 
>> 
>> local $/="}\n"; # <<<<< look here!
>> while (my $record=<DATA>) {
>> 
>>   #print "*** $record ***"; # for debugging record extracting
>> 
>>   my ($lease)=$record=~/lease\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
>>   my ($binding_state)=$record=~/^\s+binding\s+state\s+(\w+)/m;
>>   my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_]+)"/m;
>> 
>>   print "lease '$lease' (host '$client_hostname') has ".
>>          "binding state '$binding_state'\n";
>> }
> 
> This script works really well but, I am a bit confused on what you are doing
> with this: local $/="}\n";

$/ is the Input Record Separator variable.

perldoc perlvar

> I have not seen local used much as I thought it
> was replaced by "my".

It has been for user defined variables however you still have to use local for
Perl's "special" variables like $/.

> It almost looks like you are defining the end of each
> lease entry with a closing curly brace and a new line.  Does the dollar sign
> indicate that is the end of the input?

No, the dollar sign indicates that / is the name of a scalar variable.

> As for the regex matches the first two make sense to me but I am a bit
> confused on the third one
> my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_]+)"/m
> 
> I can see that we are creating a variable called $client_hostname which is
> defined by a match to $record which is feed in by the filehandle.  I see
> that we are searching for a line starting with one or more spaces followed
> by client-hostname then one or more spaces followed by one word character
> and anything else but what does the -_ do?

The hythen (-) in a character class defines a range of characters unless it is
at the beginning or end of the character class so '.-_' is the range of
characters starting at '.' and ending at '_'.  That is probably a mistake but
I would have to check the RFCs to confirm that.  Hans probably meant '[\w._-]'
instead?

> And what does the m on the
> outside do?

The /m option means that ^ will match at the beginning of a line inside the
string in $record instead of at the beginning of $record.



John
-- 
use Perl;
program
fulfillment

-- 
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