in reply to diamond operator current filename

use warnings; use strict; open OUT, '>', 'delme.txt'; print OUT <<TEXT; 1) how can someone know what is the current edited file's name ? quest +ion TEXT close OUT; @ARGV = ('delme.txt'); $^I = ".bak"; while (<>) { if ($. == 1) { print STDOUT "File geing processed is $ARGV\n"; open OUT, '>', $ARGV or die "Can't open $ARGV: $!"; print OUT <<" NEWTEXT"; 2) can someone make modifications on that file, isnt it al +ready opened by the <> diamond operator? NEWTEXT } print; print STDOUT; } close OUT; print "\n"; open IN, '<', 'delme.txt'; print <IN>; close IN;

Prints:

File geing processed is delme.txt 1) how can someone know what is the current edited file's name ? quest +ion 2) can someone make modifications on that file, isnt it al +ready opened by the <> diamond operator?

which shows that the file can be reopened. Notice though that the close must be after the magic file processing has happened in the while loop (close OUT; is after the while loop). If the close is in the same block as the open OUT the "update" gets replaced by the loop version of the update.

Not sure where that would be useful however!


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: diamond operator current filename
by spx2 (Deacon) on Jun 14, 2007 at 04:38 UTC

    hi,thanks for the answer. i wasnt able to apply what you wrote. i tried what you wrote and debbugged it with ptkdb,it seems the file is reopened, but on reading from it with $buff=<new_handle_same_file> it seems $buff is undef over and over again. about it beeing useful ... i dunno , its just an exercice randal schwartz's book learning perl , actually its from chapter 9 exercice 5,that goes with the 4th edition that i own of the book. ok...i managed to surpass the problem ,not even reading the file the 2nd time as i was planning to,and making it like this http://perlhobby.googlecode.com/svn/trunk/scripturi_perl_teste/prob_leraning_perl/p9_5.pl it seems next magically jumps the line on wich the text to be checked in the file is supposed to be and thats how i get over the problem. i would be very interested to see how one could jump to the next FILE the diamond operator is processing, that would optimize the solution much better.