I am having the darnedest time with a bug. I recently asked Perl Regular Expression for extracting multi-line LaTeX chapter name, and followed the advice to use a Perl regular expression tester until I'm satisfied with my regular expression. I committed my code and was happy.
But now, when I run the program:
#!/usr/bin/perl -i.old # In-place edit, backup as '.old'
use strict;
use warnings;
use Path::Tiny;
my $filename = shift or die "Usage: $0 FILENAME";
my $content = path($filename)->slurp_utf8;
$content =~ s/chapter/addchap/g;
path($filename)->spew_utf8($content);
I end up with an empty file. It is very strange -- variations of this substitution (like s/\\chapter/\\addchap/g;
) fail with an empty file, but dissimilar substitutions, like $content =~ s/ //g;
don't. Even stranger, the perl -i
flag isn't being respected either. What the heck is going on?
Here is some sample input:
\chapter{\texorpdfstring{{I} {Into the
Primitive}}{I Into the Primitive}}
Buck did not read the newspapers, or he would have known that trouble
was brewing, not alone for himself,
I updated my code, per the answers, but I am still getting empty files.
#!/usr/bin/perl
use strict;
use warnings;
use Path::Tiny;
my $filename = shift or die "Usage: $0 FILENAME";
my $content = path($filename)->slurp_utf8;
$content =~ s/\\chapter/\\addchap/gs;
path($filename)->spew_utf8($content);
The thing I find really confusing is that some substitutions "work", like:
$content =~ s/the/thee/g;
but even
$content =~ s/ch//g;
fails with an empty file.