[EMAIL PROTECTED] (Peter Scott) writes:

>>How can I get an editable file name in there programatically?
>
> You need a more capable ReadLine package.  Install
> Term::ReadLine::Gnu and do:
>
> BEGIN { $ENV{PERL_RL} = 'Gnu' }
> use Term::ReadLine;
> my $term = new Term::ReadLine 'Simple Perl calc';
> my $prompt = "Enter your arithmetic expression: ";
> my $OUT = $term->OUT || \*STDOUT;
> while ( defined ($_ = $term->readline($prompt, '<SOMETHING here>')) ) {
>    my $res = eval($_);
>    warn $@ if $@;
>    print $OUT $res, "\n" unless $@;
>    $term->addhistory($_) if /\S/;
> }

Haa... nifty.  Thanks, that was the missing ingrediant.

I think I'll try your patience once more and see if you'd care to
comment or provide a sanity check on the script resulting from your
input:
(Its overcommented but that might mean I'll see what its supposed to
do 6 mnths from now)


#!/usr/local/bin/perl -w

# Keywords: rena.pl - designed to hasten file renaming [ File names
# presented one at a time on an editable cmdline]
# Apr 30 23:32:40 2004 5
# &&

my $myscript;
($myscript = $0) =~ s:^.*/::;

## Make sure PERL_RL is not set to 'perl'
BEGIN { $ENV{PERL_RL} = 'Gnu' }

use Term::ReadLine;

if($ARGV[0] =~ /help/i){
   usage();
   exit;
}
## Catchall reg
my $filereg    = '[\d\w]';
## Default working dir
my $wrkdir = "./";

## ========== BEGIN Getopts section ==========
## Declare vars inside qw()
use vars qw($opt_d $opt_r );
use Getopt::Std;
my $optstr ="r:d:";
getopts($optstr);

if($opt_d){
  ## If $opt_d doesn't exist, show usage and die, else set $wrkdir
  if(! -d $opt_d){
    usage();
    die "Problem with  <$opt_d>: $!";
  }else{
    $wrkdir = $opt_d;
  }
}

if($opt_r){
  ## Change the default regex
  $filereg = $opt_r;
}
opendir(WRKDIR,"$wrkdir")or die "Can't open $wrkdir: $!"; 
chdir $wrkdir or die "Can't chdir to $wrkdir: $!";

## compile file regex
$freg = qr/$filereg/;
@FilesToRename =  grep { /$freg/ && -f "$_" }readdir(WRKDIR);
close(WRKDIR);

my $term = new Term::ReadLine 'Perl file rename utility';
my $prompt = "(type \`p <RET>' to bypass current file, press \`Ctrl-c' to abort 
program) 
Filename to edit => ";
## tell perl where to send the output
my $OUT = $term->OUT || \*STDOUT;

for(@FilesToRename){
   my ($reg, $cmpreg, $fname, $newname);
   $fname = $_;
   ## If we have input
   if ( defined ($_ = $term->readline($prompt, "$fname")) ) {
      ## Stick whatever edits into $newname
      $newname = $_;
      ## Compile a regex for testing for user bypass `p<RET>'
      $reg = $fname . "p\$";
      $cmpreg = qr/$reg/;
      ## If we don't have a `p<RET>' then rename to $newname
      if ( $newname !~ /$reg/){
        print "Renaming $fname to $_\n";
        rename $fname, $newname or warn "Can't rename $fname to $newname: $@";
        ## Bump our cmdline memory
        $term->addhistory($_) if /\S/;
      }else{
        print "No rename performed on $fname, moving on..\n";
      }
     
   }
}
sub usage {
   print <<EOM;

Purpose: Make renaming of files faster
USAGE: \`$myscript [-d 'DIR'] [-r REG]' 
Flags: -d DIR <optional> for selecting a directory to work in
       -r REG <optional> for setting the file selection regex
EOM
} 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to