in reply to Problem deleting require()'d file in SIGINT handler

I don't have the source here, therefore from the top of my hat: A few month ago I had studied this region in 5.005 for working with filters. I think the opened FH is a glob, which is stored in %INC. The $ of this glob is saved the fileno into a lot of lines earlier (code folding helps). So I think you must write:open my $x,"<&=".$*{delete $INC{$key}}

Replies are listed 'Best First'.
Re^2: Problem deleting require()'d file in SIGINT handler
by shay (Beadle) on Oct 13, 2004 at 09:15 UTC

    Did you mean something like this?:

    use strict; use warnings; my $file = 'reqtest.pl'; my $fh; open $fh, ">$file" or die "Can't create file '$file': $!: $^E\n"; print $fh <<'EOT'; BEGIN { print STDERR "Hit CTRL+C now!\n"; sleep 3; } 1; EOT close $fh; unshift @INC, sub { my($coderef, $filename) = @_; return unless $filename eq $file; open $fh, $filename or die "Can't read file '$file': $!: $^E\n"; return $fh; }; $SIG{INT} = sub { print "Caught SIGINT. Terminating.\n"; if (open my $fh2, '<&=' . $*{delete $INC{$file}}) { close $fh2 or warn "Can't close fh2: $!: $^E\n"; } else { warn "Can't fdopen fh: $!: $^E\n"; } if (-f $file) { unlink $file or warn "Can't delete file '$file': $!: $^E\n"; } else { print STDERR "file is already deleted (!)\n"; } exit; }; require $file or die "require() failed\n"; unlink $file or die "Can't delete file '$file': $!: $^E\n";

    It doesn't work :( It prints "Can't fdopen fh: Invalid argument", and if I use Data::Dumper to dump the value corresponding to $file that was delete()'d from %INC then we can see why: it shows:

    $VAR1 = sub { "DUMMY" };

    i.e. not a GLOB reference at all.

    - Steve