Harry wrote:
>>>>> In shell scripting, at least with bash, taking input from user is done
>>>>> with `read line' but you can say 'read -e line' and the input is
>>>>> suddenly done with bash completion.. and I think a few other niceties.
>>>>>
>>>>> Is there something similar in perl?

Chas O. answered
>>>> snip
>>>> Take a look at [Term::Readline][1], [Term::Readline::GNU][2], and
>>>> [Term::Readline::Perl][3].
>>>
>>> I'm sad to say after installing Term::ReadLine::Gnu and looking at the
>>> the 1014 lines of perldoc Term::ReadLine::Gnu..
>> snip

[...] snipped nifty code.

> Haa... yes of course.... a much smarter approach thanks..

I'm integrating this new stuff into my homeboy script.  In that usage
I really want to write to a final target file with a certain format.

Like:

Keywords: SOME KEY WORDS ON THIS LINE
      [Keydate:FORMATTED DATE]
any more content from the for the target file
&&

So there is a `keyword' line followed by a formatted date line.
Followed by any other commentary, followed by a `&&' delimiter on a
line by itself.

The target file is a very basic data base where I keep snippets of info
I want to be able to find later.  The file is thousands of lines long
so not something I want load.

Another homeboy script does a few different kinds of search functions
on this flat file db and spits out the hits along with the whole keywords
and date entry.

Below I've included a test script where I try to integrate your
suggestions and a version of your code that I've seriously meddled
with.

This is an edited down version of what would really go in the main
script. 

I added the file extension only because I'll be using emacs as the
invoked editor and that extension will make emacs set a few handy
things that are useful in a plain text message, like line wrapping 
and abbrev completions.  

I tinkered around trying to use the notation you used in your example
on file opening:
( `open my $fh, "<", $tmpfile' ) <and other places>

but stumbled with it and finally reverted to something I'm used to.
Mainly to get this posted and have a chance to hear any other advice
you might have.

But on that notation point, when using a variable to represent the FH,
like in the notation you showed, how do you later refer to it in a
print statement when you want to direct the print to the FH? Like:

   print FH "something\n";

=====     *     =====     *     =====     *     =====
 
Here is a test script trying to integrate what you've shown but aim it
at an intended target.  And format the input somewhat.  Using the
first line of input as the `Keywords' line.

It works in my tests and will be a welcome improvement in that much
used script... but wondering if there are better ways to accomplish 
the goal?

#!/usr/local/bin/perl

use strict;
use warnings;

use File::Temp qw/tempfile/;

my $ext = '.txt';

#this is the safest way to get a temporary filename
my $tmpfile = do {
   my ($fh, $filename) = tempfile();
   close $fh;
   $filename;
};

$tmpfile = $tmpfile . $ext;

my $editor = $ENV{EDITOR} || "/usr/bin/vim";

system($editor, $tmpfile) == 0
   or die "error occured while trying to run editor\n";

my @contents;
open(TMPFILE,"<$tmpfile")
    or die "could not open $tmpfile: $!\n";

## Where editor content is really headed
my $ActualTarget = './misc-snp';
open(ACTUAL_TARGET,">>$ActualTarget")
    or die "Can't open $ActualTarget : $!";

my $paddedds = '';
while(<TMPFILE>){
  if($. == 1){
    print ACTUAL_TARGET "Keywords: $_";
    next;
  }
  if($. == 2){
    print ACTUAL_TARGET "     [Keydate:" . PaddedDateStr() . "]\n$_";
    $paddedds = "TRUE";
    next;
  }
  print ACTUAL_TARGET $_;
} 
## If the padded date string didn't get printed yet, do so now.
if(!$paddedds){
    print ACTUAL_TARGET "     [Keydate:" . PaddedDateStr() . "]\n";
}
## Close the entry with double ampersands on a line by themselves.
print ACTUAL_TARGET  "&&\n";
close(ACTUAL_TARGET);

unlink $tmpfile
   or die "could not delete <$tmpfile>: $!";

sub PaddedDateStr {
  my ($mon,$mday,$year,$hour,$min,$sec,$wday) = 
    (localtime(time))[4,3,5,2,1,0,6];
  $year -= 100;  
  $mon  += 1;
  my $PaddedDateStr = sprintf "%02d%02d%02d%s%02d%02d%02d%s%d", $mon,
  $mday,$year,"_",$hour,$min,$sec,"_",$wday;
  return $PaddedDateStr;
}


-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to