Leser: 1
9 Einträge, 1 Seite |
Quote...
If FILEHANDLE is an undefined scalar variable (or array or hash element) the variable is assigned a reference to a new anonymous filehandle, ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/perl
# vi:ts=4 sw=4 et:
use strict;
use warnings;
my @handles = ();
for ( 0 .. $#ARGV ) {
open $handles[$_], $ARGV[$_] or die "$ARGV[$_]: $!\n";
}
print scalar(@handles) . " open Filehandles\n";
open($hande,'*',$filename)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!usr/bin/perl
use strict;
my @files=();
push(@files,{name=>'./test.txt' open=>">", $fh=>''});
push(@files,{name=>'./test2.txt' open=>"<", $fh=>''});
push(@files,{name=>'./test3.txt' open=>">>", $fh=>''});
for my $i (@files)
{
open($i->{fh},$i->{open},$i->{name}) or die "Fehler $!\n";
}
# mach irgendwas mitr den Dateihandles
for my $i (@files)
{
close($i->{fh}) or die "Fehler $!\n";
}
1
2
3
4
5
use IO::File;
my @files;
map {push @files, IO::File->new("$_")} qw/foo.txt bar.txt/;
map {print <$_>} @files;
1
2
3
4
5
6
7
8
9
use IO::File;
my @files;
map {push @files, IO::File->new("$_")} qw/foo.txt bar.txt/;
for my $file (@files) {
my $line = <$file>;
print "Das ist $line.\n";
}
my $line = $files[0]->getline;
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
41
#!/usr/bin/perl
use strict;
use IO::File;
my @files=();
push(@files,{name=>'/tmp/txt1.txt', nopen=>"r", open=>"<", fh=>undef, nfh=>''});
push(@files,{name=>'/tmp/txt2.txt', nopen=>"r", open=>"<", fh=>undef, nfh=>''});
push(@files,{name=>'/tmp/txt3.txt', nopen=>"r", open=>"<", fh=>undef, nfh=>''});
push(@files,{name=>'/tmp/txt4.txt', nopen=>"r", open=>"<", fh=>undef, nfh=>''});
push(@files,{name=>'/tmp/txt5.txt', nopen=>"r", open=>"<", fh=>undef, nfh=>''});
for my $i (@files)
{
# ALT
open($i->{fh},$i->{open},$i->{name}) or die "Fehler $!\n";
# NEU ( Objektorienetiert )
$i->{nfh}=IO::File->new($i->{name},$i->{nopen}) or die "Fehler $!\n";
}
for my $i (@files)
{
# ALT
# my $txt=readline($i->{fh}); #<= Alternativ
my $fh=$i->{fh};
my $txt=<$fh>;
print "ALT: $i->{name}: $txt\n";
# NEU
my $ntxt=$i->{nfh}->getline();
print "NEU: $i->{name}: $ntxt\n";
}
for my $i (@files)
{
# ALT
close($i->{fh}) or die "Fehler $!\n";
# NEU
$i->{nfh}->close() or die "Fehler $!\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my @FHs = ();
for my $i (0..$#ARGV) { # fuer jeden uebergebenen Parameter
open( $FHs[$i], "<", $ARGV[$i] )
or die "Error: couldn't open file '$ARGV[$i]': $!\n";
} # for
# z.B. alles in ein array einlesen
my @array = ();
foreach my $FH (@FHs) {
push( @array, <$FH> );
close( $FH ); # nicht mehr benoetigten FH schliesen
} # for
# array entsorgen, wenn da eh nichts mehr offen ist
undef( @FHs );
9 Einträge, 1 Seite |