Another thing you can do is break your larger regexes into parts to
make them more readable/maintainable.  It also helps to use the x flag
so that you can separate the individual tokens and comment them.

#!/usr/bin/perl

use strict;
use warnings;

use Regexp::Common;

#FIXME: the border value should be (foo|bar|baz) where foo,
#bar, and baz are the valid values for a border
my $border = qr{ # match a border value
        border:  #constant indicating this is a border value
        \s*      #optional spaces
        (.*)     #the border value
}xi;
my $num = qr{            #match a number of feet or inches
        ($RE{num}{real}) #a real number
        (['"]|'')        #unit indicator ' is foot, " or '' is inches
}x;
my $dim = qr{    #match a dimension
        $num     #height value
        \s*      #optional spaces
        (?:x|by) #x or by, don't capture
        \s*      #optional spaces
        $num     #width value
}xi;
my $size = qr{   #match a size value
        size:    #constant indication this is a size value
        \s*      #optional spaces
        $dim     #the dimensions
}xi;
my $tag = qr{    #match a tag value
        tag:     #constant indication this is a tag value
        \s*      #optional spaces
        (.*)     #the tag
}xi;
my $match=qr{    #match the whole record for a widget
        ^        #start of the record
        $border  #a border value
        \s*      #optional spaces
        $size    #a size value
        \s*      #optional spaces
        $tag     #a tag value
        $        #optional spaces
}x;

while(<>) {
        chomp;
        if (/$border\s*$size\s*$tag/) {
                my ($border, $h, $h_type, $w, $w_type, $tag) =
                        ($1, $2, $3, $4, $5, $6);
                print
                "Border is $border\n",
                "Height is ", ($h_type !~ /"|''/ ? $h*12 : $h), " inches\n",
                "Width is ",  ($w_type !~ /"|''/ ? $w*12 : $w), " inches\n",
                "Tag is $tag\n";
        } else {
                print "invalid entry\n";
        }
}

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