There are some odd things about your program, so I'll try to send up some points your way:
First, apart from the warning tag (-w), put this on the top of your program:
This will catch a lot of errors, mostly global variables and those out of scope.
When opening a file and reading from it, the cannonical, and better way is to write:
Code:
open(my $DAT, '<', "sentences.txt") || die "Couldn't open the file - $!\n";
foreach my $sentences (<$DAT>)
Now, in your code, this:
Code:
if ($sentences =~ m/^($pattern)/) {
warn "$pattern --- matched --- $sentences\n";
}
elsif ($sentences =~ m/\b$pattern\b/) {
warn "$pattern --- matched in word boundary --- $sentences\n";
}
doesn't work, since $pattern isn't defined anywhere.
Now, the procedure "procedure", I've pointed some of the more obvious points in comments:
Code:
sub procedure {
my ($string) = @_; # preferable my $string = shift;
warn "String is: $string\n";
my @words = split(/, (?!\W)|\s|(?<=\W) &/, $string);
warn "words are: @words\n";
my $match = "";
foreach my $word (@words) {
warn "the words after splitting are: $word\n";
my $first = substr($word, 1);
my $newword = "[A-z&]".$first; # for 'word' this prints '[A-z&]ord'
warn "word after removing first letter: $newword\n";
$match = $match." ".$newword; #$match is "", $newword is '[A-z&]ord'
warn "concatenated word is: $match\n";
}
return $match; #this is a scalar, you call it in list context ?!?
}
Hope this gives you some ideas