D
David Lebel
Hi,
as a way to teach myself Perl, I started to convert my toolbox of
scripts from (mostly) Perl to Python. One script I had in my toolbox
was used to rename files using a regular expression. That script came
from Tom Christiansen (of Perl fame) and shows the great flexibility of
Perl doing regular expression operations.
Now, I don't know where to start doing the conversion.
Any insights?
#!/usr/bin/env perl
# Tom Christiansen <[email protected]>, in alt.sources, 90-12-08.
# Modified by François Pinard <[email protected]>, 91-04-19.
#
# rename script examples from lwall:
# rename 's/\.orig$//' *.orig
# rename 'y/A-Z/a-z/ unless /^Make/' *
# rename '$_ .= ".bad"' *.f
# rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *
$operation = shift;
for (@ARGV)
{
$previous = $_;
eval $operation;
die $@ if $@;
if ($previous ne $_)
{
if (-f $_)
{
warn "$0: $_ already exists\n";
}
else
{
rename($previous, $_)
|| warn "$0: can't rename $previous to $_: $!\n";
}
}
}
as a way to teach myself Perl, I started to convert my toolbox of
scripts from (mostly) Perl to Python. One script I had in my toolbox
was used to rename files using a regular expression. That script came
from Tom Christiansen (of Perl fame) and shows the great flexibility of
Perl doing regular expression operations.
Now, I don't know where to start doing the conversion.
Any insights?
#!/usr/bin/env perl
# Tom Christiansen <[email protected]>, in alt.sources, 90-12-08.
# Modified by François Pinard <[email protected]>, 91-04-19.
#
# rename script examples from lwall:
# rename 's/\.orig$//' *.orig
# rename 'y/A-Z/a-z/ unless /^Make/' *
# rename '$_ .= ".bad"' *.f
# rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *
$operation = shift;
for (@ARGV)
{
$previous = $_;
eval $operation;
die $@ if $@;
if ($previous ne $_)
{
if (-f $_)
{
warn "$0: $_ already exists\n";
}
else
{
rename($previous, $_)
|| warn "$0: can't rename $previous to $_: $!\n";
}
}
}