10 Einträge, 1 Seite |
QuoteIn the first form, the return value of EXPR is parsed and
executed as if it were a little Perl program. The value of the
expression (which is itself determined within scalar context) is
first parsed, and if there weren't any errors, executed in the
lexical context of the current Perl program, so that any
variable settings or subroutine and format definitions remain
afterwards. Note that the value is parsed every time the "eval"
executes. If EXPR is omitted, evaluates $_. This form is
typically used to delay parsing and subsequent execution of the
text of EXPR until run time.
In the second form, the code within the BLOCK is parsed only
once--at the same time the code surrounding the "eval" itself
was parsed--and executed within the context of the current Perl
program. This form is typically used to trap exceptions more
efficiently than the first (see below), while also providing the
benefit of checking the code within BLOCK at compile time.
QuoteEs ist Bestandteil eines file renaming utilities, welches dem User gestattet seine Dateien z.B. mit regulären Ausdrücken zu verändern.
rename 'y/A-Z/a-z/' *
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use strict;
use Getopt::Long;
Getopt::Long::Configure('bundling');
my ($verbose, $no_act, $force, $op);
die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
unless GetOptions(
'v|verbose' => \$verbose,
'n|no-act' => \$no_act,
'f|force' => \$force,
) and $op = shift;
$verbose++ if $no_act;
if (!@ARGV) {
print "reading filenames from STDIN\n" if $verbose;
@ARGV = <STDIN>;
chop(@ARGV);
}
for (@ARGV) {
my $was = $_;
eval $op;
die $@ if $@;
next if $was eq $_; # ignore quietly
if (-e $_ and !$force)
{
warn "$was not renamed: $_ already exists\n";
}
elsif ($no_act or rename $was, $_)
{
print "$was renamed as $_\n" if $verbose;
}
else
{
warn "Can't rename $was $_: $!\n";
}
}
QuoteEs ist Bestandteil eines file renaming utilities, welches dem User gestattet seine Dateien z.B. mit regulären Ausdrücken zu verändern.
rename 'y/A-Z/a-z/' *
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use strict;
use Getopt::Long;
Getopt::Long::Configure('bundling');
my ($verbose, $no_act, $force, $op);
die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
unless GetOptions(
'v|verbose' => \$verbose,
'n|no-act' => \$no_act,
'f|force' => \$force,
) and $op = shift;
$verbose++ if $no_act;
if (!@ARGV) {
print "reading filenames from STDIN\n" if $verbose;
@ARGV = <STDIN>;
chop(@ARGV);
}
for (@ARGV) {
my $was = $_;
eval $op;
die $@ if $@;
next if $was eq $_; # ignore quietly
if (-e $_ and !$force)
{
warn "$was not renamed: $_ already exists\n";
}
elsif ($no_act or rename $was, $_)
{
print "$was renamed as $_\n" if $verbose;
}
else
{
warn "Can't rename $was $_: $!\n";
}
}
QuoteBei "GNU/Linux" wird mit Perl immer das Tool "rename" mitgelifert das vom Meister Larry Wall selber programmiert wurde, und genau diese Aufgabe bereits erfüllt.
QuoteBei "GNU/Linux" wird mit Perl immer das Tool "rename" mitgelifert das vom Meister Larry Wall selber programmiert wurde, und genau diese Aufgabe bereits erfüllt.
1
2
3
4
5
6
$file = "10abc.txt";
$from = "(\\d\\d).+?(\\.\\w+)";
$to = '$1$2';
$to = "qq#$to#";
$file =~ s/$from/$to/gee;
print $file, "\n";
QuoteBei mir (SuSE 9.3// Perl 5.8.6) übrigens _nicht_ mit dabei.
1
2
3
4
5
6
$ ls -lha /usr/bin/rename
lrwxrwxrwx 1 root root 24 2006-03-03 15:46 /usr/bin/rename -> /etc/alternatives/rename
$ ls -lha /etc/alternatives/rename
lrwxrwxrwx 1 root root 16 2006-03-03 15:46 /etc/alternatives/rename -> /usr/bin/prename
$ dpkg -S /usr/bin/prename
perl: /usr/bin/prename
QuoteMein Script macht aber auch:
10 Einträge, 1 Seite |