[32968] in Perl-Users-Digest
Perl-Users Digest, Issue: 4244 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 2 16:09:15 2014
Date: Wed, 2 Jul 2014 13:09:02 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 2 Jul 2014 Volume: 11 Number: 4244
Today's topics:
Re: using perl to write letters <cal@example.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 01 Jul 2014 22:06:29 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using perl to write letters
Message-Id: <c1hianFdmngU1@mid.individual.net>
On 07/01/2014 01:29 AM, $Bill wrote:
> On 6/30/2014 17:08, Randy Westlund wrote:
>> On 2014-06-30, Cal Dershowitz wrote:
>>> Hello ng,
>>>
>>> I seem to be starting over again, wanting to work up a few utilities
>>> that I look at as timesavers. I have elderly family members who aren't
>>> computer-savvy, and I want to re-use the paragraphs I've typed up for
>>> uncles 1, 2, and 3, so I've made this so that addressee and signatory
>>> come off the commandline if desired.
>>
>> If your only goal is to save time, there may be faster ways than
>> writing perl. For example, I use mutt with vim to compose emails.
>> When I want to reuse a paragraph, I write those lines out to
>> /tmp/mailtext and then just read them back in on the next email.
>
> Or simply put the content in a letter buffer and paste where needed later.
> If the earlier emails are saved in a file, then you could easily switch
> your editor to the file and cut what you need and go back to the orig file
> and paste it.
The idea here is that I want to include parts without a whole bunch of
gui events.
>
> As far as the original problem, if you read a paragraph in, you can
> use a RE to strip any WS from front and back.
> $text =~ s/^\s+//; $text =~ s/\s+$//; or maybe $text =~ s/^\s+|\s+$//gs;
Works great, $Bill, thx...
$ perl letter8.pl chuck
/home/fred/Documents/letter/texts1
pars is a.txt b.txt c.txt
pars is a.txt b.txt c.txt
dollar one is 3
dollar one is 2
dollar one is 1
dollar one is 4
matches are 1 1 2 3 4
name is /home/fred/Documents/letter/chuck5.txt
$ cat chuck5.txt
Dear chuck,
This file has a normal newline after it.
This file has extra newlines in front and in back.
This file lacks a newline at the end of this sentence.
Love,
--
John Hancock
$ cat letter8.pl
#!/usr/bin/perl
use strict;
use warnings;
use Path::Class;
use Cwd;
use v5.10;
use File::Slurp;
#usage has 2 args
my ($addressee, $signatory) = @ARGV;
if (not defined $addressee) {
$addressee= "Betsy Ross";
}
if (not defined $signatory) {
$signatory= "John Hancock";
}
my $dir = getcwd;
my $subdir = "texts1";
my $dir2 = dir($dir, $subdir);
say $dir2;
my @pars;
opendir(my $dh, $dir2) || die "can't opendir $dir2: $!";
while (my $file = readdir($dh)) {
next unless ($file =~ m/.\.txt$/);
push @pars, $file;
}
closedir($dh);
say "pars is @pars";
@pars = sort @pars;
say "pars is @pars";
# create output file
my $filetype = "txt";
my $name = next_in_type($dir,$addressee,$filetype);
say "name is $name";
open(my $fh, ">", $name)
or die "cannot open > $name: $!";
print $fh "Dear $addressee,\n";
print $fh "\n";
# print body
while (@pars) {
my $file = shift(@pars);
my $file2 = file($dir2, $file);
my $text = read_file( $file2 ) ;
# remove preceding and trailing newlines
$text =~ s/^\s+|\s+$//gs;
say $fh $text;
print $fh "\n";
}
print $fh "Love,\n";
print $fh "--\n";
print $fh "$signatory\n";
sub next_in_type {
my $dir = shift;
my $word = shift;
my $filetype = shift;
push my @matches, 1;
opendir(my $eh, $dir) || die "can't opendir $dir: $!";
while (my $file = readdir($eh)) {
next unless ($file =~ m/^$word(\d*)\.txt$/);
say "dollar one is $1";
push @matches, $1;
}
@matches = sort @matches;
say "matches are @matches";
my $pop = pop @matches;
$pop = $pop +1;
my $r = file($dir, $word.$pop.'.'.$filetype);
return $r;
}
$
This subroutine is something that I reach for a lot, as it promises a
new, unique file. I'm happy with this result for the day.
Cheers,
--
Cal Dershowitz
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4244
***************************************