- Using perl -l gives you autochomp which Tie::File has built in. From your other nodes I see you use this frequently to add a newline when you print.
- $^W = 1; and then use Warnings; a few lines later?
- Using autodie means the script stops after the unlink line even if there is a file to delete. Why not just die if -e $file; ?
- Does $| = 1; have any effect on Tie::File? With the option memory => 0 it is already not cacheing anything.
I don't understand the point of this code. Tying to a file that doesn't exist, adding a line, then deleting the newly created file doesn't seem useful.
| [reply] [d/l] [select] |
| [reply] |
Having a glitch of some kind---code is coming:). Here: How delightfully refreshing, here's mine
P.S. I fixed this for you $ perl delight.pl
Bareword found where operator expected at delight.pl line 21, near "#l
+ine 3 is now "something"
(Might be a runaway multi-line "" string starting on line 19)
(Missing operator before something?)
String found where operator expected at delight.pl line 21, at end of
+line
(Missing semicolon on previous line?)
syntax error at delight.pl line 21, near "#line 3 is now "something "
Can't find string terminator '"' anywhere before EOF at delight.pl lin
+e 21.
It now is fixed $ perl delight.pl
Can't unlink('file.txt'): No such file or directory at delight.pl line
+ 13
Can't unlink('file.txt'): No such file or directory at delight.pl line
+ 27
END failed--call queue aborted at delight.pl line 33.
But I took the following next logical extra forward step in improvement
It now runs great! #!/usr/bin/perl -l --
# really ensures warnings
BEGIN { $^W = 1; }
BEGIN { ${^WARNING_BITS} = 0; }
BEGIN { $^H |= 0x00000002 |0x00000200 |0x00000400; }
BEGIN { $/ = "\n"; $\ = "\n"; }
BEGIN { BEGIN {
use Carp::Always;
use Carp qw/ confess /;
sub unlink {
my $goners ;
while(@_){
my $file = shift;
unlink $file or confess "Can't unlink('$file'): $!";
$goners++;
}
return $goners;
}
} }
local $SIG{__DIE__} = sub {
confess "Uncaught exception: @_" unless $^S;
};
use strict; use warnings; use autodie;
use Tie::File;
my $file = "file.txt";
1 while unlink $file;
my $o = tie my @lines, 'Tie::Tile', $file, memory => 0;
for (@lines) {
$lines[42] = "something else is here now";
print $lines[42];
#line 42 is now "something else is here now".
}
END { END { END{ END {
undef $o;
untie @lines;
1 while unlink $file;
#really clears and empties everything.
} } } }
__END__
The output is twice as much Uncaught exception: Can't unlink('file.txt'): No such file or director
+y at realdelight.pl line 14.
main::__ANON__('file.txt') called at (eval 49) line 28
main::__ANON__('file.txt') called at realdelight.pl line 31
at realdelight.pl line 22.
main::__ANON__('Can\'t unlink(\'file.txt\'): No such file or direc
+tory at rea...') called at C:/perl/site/5.14.1/lib/Carp.pm line 101
Carp::confess('Can\'t unlink(\'file.txt\'): No such file or direct
+ory') called at realdelight.pl line 14
main::__ANON__('file.txt') called at (eval 49) line 28
main::__ANON__('file.txt') called at realdelight.pl line 31
Can't unlink('file.txt'): No such file or directory at realdelight.pl
+line 14.
main::__ANON__('file.txt') called at (eval 49) line 28
main::__ANON__('file.txt') called at realdelight.pl line 42
main::END() called at C:/perl/site/5.14.1/lib/Carp.pm line 101
eval {...} called at realdelight.pl line 14.
main::__ANON__('file.txt') called at (eval 49) line 28
main::__ANON__('file.txt') called at realdelight.pl line 42
main::END() called at C:/perl/site/5.14.1/lib/Carp.pm line 101
eval {...} called at C:/perl/site/5.14.1/lib/Carp.pm line 101
END failed--call queue aborted at realdelight.pl line 101.
| [reply] [d/l] [select] |