dg-x/ptouch.pl
2022-03-04 19:29:34 -08:00

47 lines
903 B
Perl
Executable file

#!/usr/bin/env perl
use strict;
use utf8;
use Getopt::Std;
use Tie::File;
$::VERSION = "1.0.0";
getopts("ne", \my %opts);
my $metavar;
if (defined $opts{n}) {
# "new" post
$metavar = "created";
} elsif (defined $opts{e}) {
# "edited" post
$metavar = "modified";
} else {
die "Neither -n[ew] nor -e[dited] are defined!";
}
my $timestamp = localtime;
my $fname = shift @ARGV;
tie my @fharr, 'Tie::File', $fname or die $!;
my $done = 0;
for (@fharr) {
if (s/^$metavar=.+/$metavar=$timestamp/) {
print "$fname:$.: Overwriting existing \`$metavar\'\n";
$done++;
}
if (s/^(---)$/$metavar=$timestamp\n$1/) {
print "$fname:$.: Inserting new \`$metavar\'\n";
$done++;
}
last if ($done);
}
if (!$done) {
print "$fname: No content and no \`$metavar\', inserting at end\n";
push @fharr, "$metavar=$timestamp";
}
untie @fharr;