Perl regex function call

A

alidixon

I've been having some trouble with regex's.
In the following example, the first call to extract() prints the value
I expect - 'string'.
However, the second call fails to find a match in the regex, and
displays the error:
Use of uninitialized value in concatenation (.) or string at test.pl
line 22.
''

I'm using windows XP, and tested this on perl v5.8.7 and v5.8.8.

Kind Regards

Ali

#!/usr/bin/perl

use warnings;
use strict;


my @names;

my $temp = "/testbench/string";
extract($temp);

my $temp1 = "/testbench/string1/string2/string3";
extract($temp1);


sub extract {
my $string = shift;

print ("extract::string: $string.\n ");
my $name;
($name) = ($string =~ ?^.*/(\w+)$?);
print ("'$name'\n");
}
 
P

Paul Lalli

I've been having some trouble with regex's.
In the following example, the first call to extract() prints the value
I expect - 'string'.
However, the second call fails to find a match in the regex, and
displays the error:
Use of uninitialized value in concatenation (.) or string at test.pl
line 22.
''
#!/usr/bin/perl
use warnings;
use strict;
GOOD!

my @names;

my $temp = "/testbench/string";
extract($temp);

my $temp1 = "/testbench/string1/string2/string3";
extract($temp1);


sub extract {
my $string = shift;

print ("extract::string: $string.\n ");
my $name;
($name) = ($string =~ ?^.*/(\w+)$?);

Why are you using ?? as the delimiters for your pattern match? Are you
aware that those are "special"? Did you want that special behavior?
From perldoc perlop:
?PATTERN?
This is just like the "/pattern/" search, except
that it matches only once between calls to the
reset() operator.
print ("'$name'\n");

Never ever ever use the results of a pattern match without first
insurring that your pattern match succeeded.

if (my ($name) = ( $string =~ m{^.*/(\w+)$} ) {
print "Match succeeded: '$name'\n";
} else {
print "Match failed\n";
}

Note, please, that the pattern of ^.* is wholly meaningless. "The
beginning of string followed by anything" will *always* match every
string. Your pattern can be reduced to:
m{/(\w+)$}

Paul Lalli
 
D

David Squire

I've been having some trouble with regex's.
In the following example, the first call to extract() prints the value
I expect - 'string'.
However, the second call fails to find a match in the regex,

The problem is the choice of regex delimiters, since this does what you
expect:

sub extract {
my $string = shift;

print ("extract::string: $string.\n ");
my $name;
($name) = ($string =~ /^.*\/(\w+)$/);
print ("'$name'\n");
}

.... but this is not an explanation :(
and displays the error:
Use of uninitialized value in concatenation (.) or string at test.pl
line 22.

This is due to the fact that $name never gets initialized on line 21
when the regex doesn't match.

DS
 
M

Mintcake

I've been having some trouble with regex's.
In the following example, the first call to extract() prints the value
I expect - 'string'.
However, the second call fails to find a match in the regex, and
displays the error:
Use of uninitialized value in concatenation (.) or string at test.pl
line 22.
''

I'm using windows XP, and tested this on perl v5.8.7 and v5.8.8.

Kind Regards

Ali

#!/usr/bin/perl

use warnings;
use strict;


my @names;

my $temp = "/testbench/string";
extract($temp);

my $temp1 = "/testbench/string1/string2/string3";
extract($temp1);


sub extract {
my $string = shift;

print ("extract::string: $string.\n ");
my $name;
($name) = ($string =~ ?^.*/(\w+)$?);
print ("'$name'\n");
}

perl -f reset

Don't use ?? match delimiters if you don't want the behaviour you're
getting
 
D

DJ Stunks

Paul said:
Why are you using ?? as the delimiters for your pattern match? Are you
aware that those are "special"? Did you want that special behavior?

?PATTERN?
This is just like the "/pattern/" search, except
that it matches only once between calls to the
reset() operator.

this was news to me, so I read perldoc -f reset, but I was wondering if
someone had a quick example of where you would want to take advantage
of this behavior?

The only time I've used a CONTINUE block is when I use -p :)

TIA,
-jp
 
P

Paul Lalli

DJ said:
this was news to me, so I read perldoc -f reset, but I was wondering if
someone had a quick example of where you would want to take advantage
of this behavior?

#!/usr/bin/perl
use warnings;
use strict;

while (<DATA>){
if (?^Foo?) {
print "First line that foo starts is $.\n";
}
#do other stuff with every line...
}

__DATA__
stuff
more stuff
Foo stuff
more stuff
Foo more stuff
Foo almost done
no more


Output:
First line that foo starts is 3


Could you do this without the magical m?? syntax? Sure:
#!/usr/bin/perl
use warnings;
use strict;

my $found = 0;
while (<DATA>){
if (/^Foo/ and !$found) {
print "First line that foo starts is $.\n";
$found = 1;
}
#do other stuff with every line...
}

__DATA__
stuff
more stuff
Foo stuff
more stuff
Foo more stuff
Foo almost done
no more


But as you can see, it's longer and involves extra variables. The m??
syntax should, in my opinion, be seen as a shortcut, and nothing more.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top