Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: a word of warning about /$pattern/

by Roger (Parson)
on Dec 03, 2003 at 02:51 UTC ( [id://311811]=note: print w/replies, xml ) Need Help??


in reply to a word of warning about /$pattern/

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 { $_ ||= '.*' } @_; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://311811]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found