¿Hay alguna forma con las bibliotecas estándar de Perl para abrir un archivo y editarlo, sin tener que cerrarlo y luego abrirlo de nuevo? Todo lo que sé hacer es leer un archivo en una cadena, cerrar el archivo y luego sobrescribir el archivo con uno nuevo; o leer y luego anexar al final de un archivo.Abrir archivo para leer y escribir (no anexar) en perl
Actualmente funciona pero; Tengo que abrirlo y cerrarlo en dos ocasiones, en lugar de una vez:
#!/usr/bin/perl
use warnings; use strict;
use utf8; binmode(STDIN, ":utf8"); binmode(STDOUT, ":utf8");
use IO::File; use Cwd; my $owd = getcwd()."/"; # OriginalWorkingDirectory
use Text::Tabs qw(expand unexpand);
$Text::Tabs::tabstop = 4; #sets the number of spaces in a tab
opendir (DIR, $owd) || die "$!";
my @files = grep {/(.*)\.(c|cpp|h|java)/} readdir DIR;
foreach my $x (@files){
my $str;
my $fh = new IO::File("+<".$owd.$x);
if (defined $fh){
while (<$fh>){ $str .= $_; }
$str =~ s/(|\t)+\n/\n/mgos;#removes trailing spaces or tabs
$str = expand($str);#convert tabs to spaces
$str =~ s/\/\/(.*?)\n/\/\*$1\*\/\n/mgos;#make all comments multi-line.
#print $fh $str;#this just appends to the file
close $fh;
}
$fh = new IO::File(" >".$owd.$x);
if (defined $fh){
print $fh $str; #this just appends to the file
undef $str; undef $fh; # automatically closes the file
}
}
1k + vistas y solo 1 voto popular. . . – GlassGhost
2 upvotes ahora: D – GLES