dg-x/ptouch.pl

47 lines
902 B
Perl
Raw Normal View History

2022-02-07 06:47:31 +00:00
#!/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!";
}
2022-03-05 04:45:29 +00:00
my $timestamp = time;
2022-02-07 06:47:31 +00:00
my $fname = shift @ARGV;
tie my @fharr, 'Tie::File', $fname or die $!;
my $done = 0;
for (@fharr) {
2022-03-05 03:29:34 +00:00
if (s/^$metavar=.+/$metavar=$timestamp/) {
2022-02-07 06:47:31 +00:00
print "$fname:$.: Overwriting existing \`$metavar\'\n";
$done++;
}
if (s/^(--.+?--)$/$metavar=$timestamp\n$1/) {
2022-02-07 06:47:31 +00:00
print "$fname:$.: Inserting new \`$metavar\'\n";
$done++;
}
last if ($done);
}
if (!$done) {
print "$fname: No content and no \`$metavar\', inserting at end\n";
2022-03-05 03:29:34 +00:00
push @fharr, "$metavar=$timestamp";
2022-02-07 06:47:31 +00:00
}
untie @fharr;