On 3/10/09 Tue Mar 10, 2009 7:59 AM, "Dermot" <paik...@googlemail.com> scribbled:
> Hi, > > I am not getting the results that I expect from this test and I am not > sure why. If I run the script below I get: > > 1..3 > Line=???/FOO BAR, Name=Joe Smo M="???" > ok 1 - handle_name ???/FOO BAR > Line=change accordingly /FOO BAR, Name=Foo bar M="change" > ok 2 - handle_name change accordingly /FOO BAR > Line=GEOF KID/FOO BAR, Name=Geoff Kidd M="" > Use of uninitialized value in concatenation (.) or string at > credit_handle.t line 33. > not ok 3 - handle_name GEOF KID/FOO BAR > # Failed test 'handle_name GEOF KID/FOO BAR' > # at credit_handle.t line 16. > # Looks like you failed 1 test of 3. > > > The 'GOOF KID' entry is not getting handled correctly (or rather as I > want). It is being processed within the initial if() block. It was my > understanding that a failed match would mean control would fall to the > else statement. If I uncomment the 2 lines below, I can make it would > but I suspect there is something I am not seeing here. I have used > this construct a lot in the past and it's been fine. The match does not fail. In fact, your regex will always match any string. One of the alternatives is equivalent to /^\?*/, which means "zero or more question mark characters at the beginning of the string" and will always be true. Change this to /^\?+/ meaning "one or more ..." and you should get the results you are expecting. The alternative works because after the match $& contains what has matched, in this case the empty string '', which evaluates to false, even though you have actually matched your R.E. > ----------- handle_name.t ---------- > use strict; > use warnings; > use Test::More tests => 3; > > my %tests = ( > 'FOO BAR/FOO BAR' => ['change accordingly /FOO BAR', 'Foo bar'], > 'JOE SMO/FOO BAR' => ['???/FOO BAR',' Joe Smo'], > 'GOOF KID/FOO BAR' => ['GOOF KID/FOO BAR', 'Gooff Kid' ], > ); > > foreach my $test (keys %tests) { > my $got = handle_name($tests{$test}); > ok( $got eq $test, "handle_name ${$tests{$test}}[0]"); > } > > sub handle_name { > my $arryref = shift; > my ($line, $name) = ($arryref->[0], $arryref->[1]); > $name =~ s/^\s+//; > $name =~ s/\s+$//; > my $regex = qr/^(change|\?*)/i; > # $line =~ /$regex/; > # if ($&) { > if ($line =~ /$regex/ ) { > print STDERR "Line=$line, Name=$name M=\"$&\"\n"; > my ($nil, $rest) = ($line =~ /^(change\s+\w+\s?|\?*)(\/.*)/); > return(uc($name).$rest); > } > else { > return $line; > } > } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/