CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Test Regex Does not Work

 



stunbox
New User

Oct 28, 2009, 3:19 AM

Post #1 of 6 (774 views)
Test Regex Does not Work Can't Post

Hey There, I need your help because I don't understand the behaviour of my code. It seems buggy.


code

Quote
$hash = {
"s" => "",
"d" => "19.28.18.28",
"p1" => "",
"p2" => "88"
};



$s="192.168.3.79";
$d="19.28.18.28";
$p1="3269";
$p2="88";


print $s." -> ".$$hash{"s"}."\n";
if ( $s =~ $$hash{ "s" }) { print "\t1 ok\n";}else { print "\t1 NOK\n";}

print $d." -> ".$$hash{"d"}."\n";
if ($d =~ $$hash{ "d" }){ print "\t2 ok\n";}else{ print "\t2 NOK\n";}

print $p1." -> ".$$hash{"p1"}."\n";
if ($p1 =~ $$hash{ "p1"}){ print "\t3 ok\n";}else{ print "\t3 NOK\n";}

print $p2." -> ".$$hash{"p2"}."\n";
if($p2 =~ $$hash{ "p2" }) { print "\t4 ok\n";}else { print "\t4 NOK\n";}



Execution


Quote
192.168.3.79 ->
1 ok
19.28.18.28 -> 19.28.18.28
2 ok
3269 ->
3 NOK
88 -> 88
4 ok



And if I comment the second test it prints:

Quote
192.168.3.79 ->
1 ok
19.28.18.28 -> 19.28.18.28
3269 ->
3 ok
88 -> 88
4 ok


Why the third test could be one time true and the other time false ?


Thanks,


savo
User

Oct 28, 2009, 4:28 AM

Post #2 of 6 (768 views)
Re: [stunbox] Test Regex Does not Work [In reply to] Can't Post

I don't have an answer atm but it does the same for me, also if you change the value in $d it will then make the 3rd test true?

I will try rewriting it from scratch and see if its the same because i cannot see an error (I am only a beginner my self though).


savo
User

Oct 28, 2009, 5:00 AM

Post #3 of 6 (762 views)
Re: [savo] Test Regex Does not Work [In reply to] Can't Post

replacing =~ with eq fixes it.

Also are you setting the hash up correctly i get no warnings doing it your way but shouldn't it be


Code
my %hash = ( 
"s" => "",
"d" => "19.28.18.28",
"p1" => "",
"p2" => "88"
);



stunbox
New User

Oct 28, 2009, 5:23 AM

Post #4 of 6 (758 views)
Re: [savo] Test Regex Does not Work [In reply to] Can't Post

Thanks scavo but I want to keep the regex test, even if in its exemple I don't need it.
I 've got the explanation of that weird behaviour from almut on perlmonk's website.

Quote
m/PATTERN/cgimosx (...) If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead.

Then, contemplate on what the effects of this (somewhat odd) feature are with your $$hash{"p1"} being empty...



(This post was edited by stunbox on Oct 28, 2009, 6:05 AM)


savo
User

Oct 28, 2009, 5:36 AM

Post #5 of 6 (755 views)
Re: [stunbox] Test Regex Does not Work [In reply to] Can't Post

Makes an odd kind of sense. Thanks for letting me know the answer


FishMonger
Veteran / Moderator

Oct 28, 2009, 6:14 AM

Post #6 of 6 (745 views)
Re: [stunbox] Test Regex Does not Work [In reply to] Can't Post


Quote
I don't understand the behaviour of my code. It seems buggy.

....
....
....

Quote
I want to keep the regex test, even if in its exemple I don't need it.


That code is buggy, so why would you want to use it?

Why aren't you using the strict and warnings pragmas?

There's no need to use a hash ref in this small example, but could be useful if it's part of a larger more complex script.

I agree with savo, using the regex binding operator is an odd choice in this code.

Is there a good reason why you would not do it this way?

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $hash = {
s => "",
d => "19.28.18.28",
p1 => "",
p2 => "88"
};

my $s = "192.168.3.79";
my $d = "19.28.18.28";
my $p1 = "3269";
my $p2 = "88";

print "$s -> $hash->{'s'}\n";
print $s eq $hash->{'s'} ? "\t1 ok\n" : "\t1 NOK\n";

print "$d -> $hash->{'d'}\n";
print $d eq $hash->{'d'} ? "\t1 ok\n" : "\t1 NOK\n";

print "$p1 -> $hash->{'p1'}\n";
print $p1 eq $hash->{'p1'} ? "\t1 ok\n" : "\t1 NOK\n";

print "$p2 -> $hash->{'p2'}\n";
print $p2 eq $hash->{'p2'} ? "\t1 ok\n" : "\t1 NOK\n";


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives