spx2 has asked for the wisdom of the Perl Monks concerning the following question:

consider the inline editing like this

$^I = ".bak"; while (<>) { s/Randall/Randal/g; print; }

question 1) how can someone know what is the current edited file's name ? question 2) can someone make modifications on that file, isnt it already opened by the <> diamond operator?

Replies are listed 'Best First'.
Re: diamond operator current filename
by GrandFather (Saint) on Jun 14, 2007 at 02:25 UTC
    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

      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.

Re: diamond operator current filename
by cdarke (Prior) on Jun 14, 2007 at 07:56 UTC
    Current filename using the "diamond operator" is in $ARGV.
    <> is hiding the use of the ARGV file handle, so <> is the same as <ARGV>.
    Whether someone else can write to the file will depend on the file locking used in the implementation. Surprisingly on the implementations I have tried this on the (hidden) open allows another process to write to the file at the same time, and we read garbled data.