[pm-h] relocate metatags in hyperlatex-generated HTML
Russell L. Harris
rlharris at oplink.net
Tue Feb 13 16:10:53 PST 2007
Success! The following code does the job of relocating the meta tags:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#!/usr/bin/perl -i.bak
# 20070213 2325 gmt
use strict;
use warnings;
undef $/;
while(my $file = <>)
{
my $metatags = q{};
if($file =~ s{(<meta\s+name=["']description["'][^>]*>)}{}sm)
{
$metatags .= "$1\n";
}
if($file =~ s{(<meta\s+name=["']keywords["'][^>]*>)}{}sm)
{
$metatags .= "$1\n";
}
$file =~ s{(</title>)}{$1\n$metatags}sm;
print $file;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
However, looking at the code, I see that I don't understand the
working of the diamond operator in the context of the while structure.
Following is the explanation which I have been able to piece together
regarding of the diamond operator:
The diamond operator <filehandle> is the line-input operator; when
empty ( <> ), the diamond operator reads from the ARGV filehandle,
which reads the array of filenames from the Perl command line.
Each invocation of <> returns as a string from the filehandle the
next record of the file (as determined by the input record
separator), until the end of the file is reached, at which point
<> returns undef.
It appears that each of the three pattern-matching operators within
while structure is able to read the file from start to finish. So it
appears that, in this context, the while structure is acting simply as
a "file open" mechanism, rather than a mechanism which steps through a
file record by record.
And what happens if I should invoke the script with the command line:
$ ./metamove.pl *.html
rather than
$ ./metamove.pl just_one_file.html
? The documentation I thus far have found appears to say that <>
reads a series of files as if they were a single contiguous file.
RLH
More information about the Houston
mailing list