You would definitely want more than just the pattern you are searching for, especially when processing a form. I will normally also search for some anchors in the form fields.

Example 1 - With anchor fields
use strict; use warnings; use Data::Dumper; my @forms = ( { name => 'Name: Roger', address => 'Address: Melbourne', phone => 'Telephone: 12345', }, { name => 'Name: Roger', address => 'Address:', phone => 'Telephone: 12345', }, { name => 'Name: Roger', address => 'Address: Melbourne', phone => 'Telephone: 54321', }, ); my $m_name = 'Roger'; my $m_address = ''; # means don't care my $m_phone = '12345'; for my $form (@forms) { print Dumper($form); if ($form->{name} =~ /Name:\s*?$m_name/ && $form->{address} =~ /Address:\s*?$m_address/ && $form->{phone} =~ /Telephone:\s*?$m_phone/) { print "Match\n"; } else { print "Not match\n"; } }
And the output -
$VAR1 = { 'address' => 'Address: Melbourne', 'phone' => 'Telephone: 12345', 'name' => 'Name: Roger' }; Match $VAR1 = { 'address' => 'Address:', 'phone' => 'Telephone: 12345', 'name' => 'Name: Roger' }; Match $VAR1 = { 'address' => 'Address: Melbourne', 'phone' => 'Telephone: 54321', 'name' => 'Name: Roger' }; Not match

Ok, another example without the anchors -

Example 2 - Without anchor fields
use strict; use warnings; use Data::Dumper; my @forms = ( { name => 'Roger', address => 'Melbourne', phone => '12345', }, { name => 'Roger', address => '', phone => '12345', }, { name => 'Roger', address => 'Melbourne', phone => '54321', }, ); my $m_name = 'Roger'; my $m_address = ''; # means don't care my $m_phone = '12345'; for my $form (@forms) { print Dumper($form); my ($t_name, $t_address, $t_phone) = taint_fields($m_name, $m_address, $m_phone); if ($form->{name} =~ /$t_name/ && $form->{address} =~ /$t_address/ && $form->{phone} =~ /$t_phone/) { print "Match\n"; } else { print "Not match\n"; } } sub taint_fields { return map { $_ ||= '.*' } @_; }

In reply to Re: a word of warning about /$pattern/ by Roger
in thread a word of warning about /$pattern/ by dada

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.