I am reading the book "Beginning Perl" by James Lee and have been pretty happy with the content so far. That said I am stuck on chpt 7 which deals with regular expressions - I can't get the backreferences to work even when I copy the source verbatim.
source:
#!/usr/bin/perl -w
# matchtest2.pl
use strict;
$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)';
print "Enter a regular expression: ";
my $pattern = <STDIN>; chomp($pattern);
if (/$pattern/) {
print "The text matches the pattern '$pattern'.\n";
print "\$1 is '$1'\n" if defined $1;
print "\$2 is '$2'\n" if defined $2;
print "\$3 is '$3'\n" if defined $3;
print "\$4 is '$4'\n" if defined $4;
print "\$5 is '$5'\n" if defined $5;
} else {
print "'$pattern' was not found.\n";
}
Printing the $& variable displays 409 with expression \b\w{3}\b, but the $1 variable shows nothing.
I'm using perl, v5.10.0 built for i486-linux-gnu-thread-multi
Is this syntax outdated or obsolete?
Any and all help is appreciated.