[pm-h] write to empty files

Mark Allen mrallen1 at yahoo.com
Thu Jun 7 23:31:30 PDT 2018


This kind of task is perfect for the module “Path::Tiny”

It has all of the functionality you’re talking about except the date stuff, and for that I’d probably choose Time::Piece because it’s shipped with core perl and can do date formatting easily. (See perldoc Time::Piece for all the details.) Path::Tiny *should* be in core Perl IMO but it’s a quick and dependency free download.

use 5.014;
use Path::Tiny;
use Time::Piece;

my $t = localtime; # Time::Piece object
say “$t”; # Fri Jun  8 01:25:46 2018

my $dir = path(“.”);
for my $f ( $dir->children() ) {
   $f->spew_utf8(“project xyz\n$t\n”)
}

Sent from Mail for Windows 10

From: rlharris at oplink.net
Sent: Thursday, June 7, 2018 11:58 PM
To: Houston Perl Mongers
Subject: [pm-h] write to empty files


My need is to write a string to each file in a directory (the same string,
such as, "project xyz", needs to be written to each file).  The files have
been created with "touch", run from a bash script; so the file length is
zero.  My system is Debian.

The operation "s//project xyz/" fails on a file of zero length. My Perl
skills are rusty, and I have spent more than an hour searching the web
without success, looking for a solution.

Even more useful to me would be the ability to write to each file in a
directory a string or a set of lines which include the current date and
the filename or the full filepath:

    project xyz
    documentname.tex
    2018.06.07 1950gmt

    project xyz
    /home/rlh/scratch/documentname.tex
    2018.06.07 1950gmt
_______________________________________________
Houston mailing list
Houston at pm.org
http://mail.pm.org/mailman/listinfo/houston
Website: http://houston.pm.org/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/houston/attachments/20180608/f7da5868/attachment.html>


More information about the Houston mailing list