I've put your code in a file called broken
. I can then check the syntax by running:
$ perl -c broken
This gives the following errors:
Unrecognized escape \T passed through in regex; marked by <-- HERE in m/\nnnn\-\nn\-\nn\T <-- HERE \nn\:\nn\:\nn)
{ s/ at broken line 11.
Unmatched ) in regex; marked by <-- HERE in m/\nnnn\-\nn\-\nn\T\nn\:\nn\:\nn) <-- HERE
{ s/ at broken line 11.
The first is really only a warning. It's telling you that \T
isn't recognised as a special escape sequence in Perl regular expressions. As we want to match a "T", we can just remove the \
.
The second error is more serious, but it's easier to fix. It says that you have an unmatched )
at the end of your regex. Of course, that )
is supposed to be the end of the if (...)
statement. So why is Perl seeing it as part of the regex? Well, it's because the end of the regex needs to be marked with a closing /
character - which you have missed. Let's add it back, so the regex line in your code looks like this:
if ($_ =~ m/\nnnn\-\nn\-\nnT\nn\:\nn\:\nn/)
We can then try the syntax check again. We get another message:
Missing right curly or square bracket at broken line 12, at end of line
syntax error at broken line 12, at EOF
As there are no square brackets in the code, the problem will be with curly brackets. Of course, all curly brackets need to be balanced (for each opening bracket we need a matching closing bracket) and it doesn't take much investigation to see that you've missed a closing curly bracket somewhere. You have two opening curly brackets (one for the while
and one for the if
) and only one closing curly bracket. So let's add another one and try again.
Your code now looks like this:
use warnings;
use strict;
my $filename = 'C:\Users\M\Desktop\products.txt';
# in which strings like this are set: 'aaaa-mm-ddThh:mm:ss' ex. 1991-06-19T00:00:01
open my $IN, '<', $filename or die $!;
while (<$IN>) {
if ($_ =~ m/\nnnn\-\nn\-\nnT\nn\:\nn\:\nn/)
{ s/^(\ )=.*/"1.0"^^xsd:date/; }
}
And running the syntax check tells us:
broken syntax OK
('broken' is, of course, the filename. It's not saying we have broken syntax!)
So the syntax is now all fixed. Any remaining problems are with the semantics - what your code is actually trying to do. You haven't explained what you're trying to achieve particularly well (it would be nice to have more sample inputs and their associated outputs) but I can, at least, point out that your regex won't match what you think it matches. You seem to think that n
is a special character in Perl regexes that matches a digit. That's not the case. And n
in a regex matches the character "n". To match a digit, we use \d
. You can get more information from the regex tutorial or the regex manual page.