Path: utzoo!attcan!uunet!mcsun!hp4nl!ruuinf!piet From: piet@cs.ruu.nl (Piet van Oostrum) Newsgroups: comp.lang.perl Subject: Re: What is perl? Message-ID: <4130@ruuinf.cs.ruu.nl> Date: 29 Oct 90 11:07:37 GMT References: <1990Oct27.000801.5853@ecn.purdue.edu> Sender: news@ruuinf.cs.ruu.nl Reply-To: piet@cs.ruu.nl (Piet van Oostrum) Organization: Dept of Computer Science, Utrecht University, The Netherlands Lines: 86 In-reply-to: wscott@ecn.purdue.edu (Wayne H Scott) >>>>> In article <1990Oct27.000801.5853@ecn.purdue.edu>, wscott@ecn.purdue.edu (Wayne H Scott) (WHS) writes: WHS> I thought if I had a short utility written in perl that is useful, WHS> takes advantage of perl's features, and simply done, I could WHS> convince people. I think the following script meets your requirements: (I call it rename) You can use it for things like renaming all *.p files to *.pas ------------------------------------------------------------------------ #! /usr/bin/perl do "getopts.pl" || die "Can't include getopts.pl"; do Getopts('cdhilmnqrsC'); if ($opt_h) { print < yes; n => no; q => quit; a => all) -m execute a "mv" for each change rather than the rename syscall useful for moving across file systems or to a directory -c copy rather than rename (using "cp") -C remote copy using "rcp" -r with -m, -c or -C, gives -r option to the mv/cp command -l link rather than rename -s make symbolic links Generally, the safer options take precedence over the less safe ones. END exit; } die "Usage: rename [-cCdilmnqrs] s/x/y/ file ...\n rename -h for help\n" unless @ARGV; $expr = shift; $opt_d += $opt_n; $opt_q = 0 if $opt_i || $opt_d; $rflag = $opt_r ? " -r " : " "; $args = @ARGV; while (defined ($_ = $args ? shift(ARGV) : )) { chop unless $args; $old = $_; eval $expr; if ($@) { $_ = $@; s/file.*tokens//; die "$_\n"; } $eol = $opt_i > $opt_d ? "? " : "\n"; $new = $_; if ($old ne $new) { print "$old -> $new$eol" unless $opt_q; unless ($opt_d || $opt_i && ! &confirm) { if ($opt_s) { $res = symlink ($old, $new); } elsif ($opt_l) { $res = link ($old, $new); } elsif ($opt_C) { $res = ! system "rcp$rflag$old $new"; } elsif ($opt_c) { $res = ! system "cp$rflag$old $new"; } elsif ($opt_m) { $res = ! system "mv$rflag$old $new"; } else { $res = rename ($old, $new); } warn "error on $new\n" unless $res; } } } sub confirm { # ask for confirmation ($ans = ) =~ s/ *//; exit if ($ans =~ /^q/i); $opt_i = 0 if ($ans =~ /^a/i); return ($ans =~ /^[ay]/i); } ------------------------------------------------------------------------ -- Piet* van Oostrum, Dept of Computer Science, Utrecht University, Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands. Telephone: +31 30 531806 Uucp: uunet!mcsun!ruuinf!piet Telefax: +31 30 513791 Internet: piet@cs.ruu.nl (*`Pete')