in reply to Easy way to check if a file is open needed.

Why do you need it? Most processes don't care about the name of a file they've already opened. The filehandle stays connected to it even if the file name changes:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; open my $OUT, '>', '1' or die $!; my $child = fork; if ($child) { sleep 1; my %new_name = map +($_ => $_ + 1), 1 .. 8; for my $old (1 .. 8) { rename $old, $new_name{$old} or die $!; say "$old renamed to $new_name{$old}."; sleep 1; } exit } defined $child or die "Can't fork"; for (1 .. 10) { say {$OUT} $_; say "Printed: $_."; sleep 1; }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Easy way to check if a file is open needed. -- MSWin32
by Discipulus (Canon) on Apr 09, 2019 at 07:06 UTC
    Hello choroba,

    when I read

    > The filehandle stays connected to it even if the file name changes

    I said: oh what good news! But when I tried it appears to behaves differently on MSWin32: I suddendly get Permission denied at writetorenamed.pl line 13. after had printed 1 or 2 and the file get not renamed ;(

    perl writetorenamed.pl Printed: 1. Printed: 2. Permission denied at writetorenamed.pl line 13. Printed: 3. Printed: 4. Printed: 5. Printed: 6. Printed: 7. Printed: 8. Printed: 9. Printed: 10. cat 1 1 2 3 4 5 6 7 8 9 10

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Yes, my comment is valid only for *nix and macOS.
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: Easy way to check if a file is open needed.
by Apt_Addiction (Novice) on Apr 09, 2019 at 09:22 UTC

    Thank you for your reply. It's taken me a while to reply to this because I can't work out what a fair bit of it's doing!

    I've not come across map before, so I'll have a look at this today and learn.

    Why do I need it? It's just so that a file that's open in MS Word, for example, doesn't get renamed.