select()ing a lexical FH

  • Thread starter Tassilo v. Parseval
  • Start date
T

Tassilo v. Parseval

Also sprach Michele Dondi:
A few days ago I was about to answer a post in which a user asked for
advice on creating per-directory reports with File::Find.

Amongst the possible solutions that came to my mind, I though of using
something like this (tested!):

#!/usr/bin/perl -l
# Oversimplified example

use strict;
use warnings;
use File::Find;

File::Find::find { preprocess => sub {
open my $fh, '>', '_report.txt' or
die "Can't open `$File::Find::dir/_report.txt': $!\n";
select $fh;
@_ },
wanted => sub {
print;
}
}, @ARGV;

__END__

Eventually, I didn't post it for (i) it was probably too late, and
good answers had already been given to the OP, (ii) I was not 100%
sure about this approach.

Now my question is if there are possible issues with select()ing a
lexical filehandle: it is my understanding that it won't cease to
exist till references to it exists too, and select() seems to do just
this, keeping it in life, but is this guaranteed to continue working
in future releases of perl?

I think it's pretty safe to expect that. select() will increment the
reference-count of its argument and decrement the ref-count of the
previous default handle when a new handle is selected.

I am pretty sure that the porters wont change that in future.

Tassilo
 
M

Michele Dondi

A few days ago I was about to answer a post in which a user asked for
advice on creating per-directory reports with File::Find.

Amongst the possible solutions that came to my mind, I though of using
something like this (tested!):

#!/usr/bin/perl -l
# Oversimplified example

use strict;
use warnings;
use File::Find;

File::Find::find { preprocess => sub {
open my $fh, '>', '_report.txt' or
die "Can't open `$File::Find::dir/_report.txt': $!\n";
select $fh;
@_ },
wanted => sub {
print;
}
}, @ARGV;

__END__

Eventually, I didn't post it for (i) it was probably too late, and
good answers had already been given to the OP, (ii) I was not 100%
sure about this approach.

Now my question is if there are possible issues with select()ing a
lexical filehandle: it is my understanding that it won't cease to
exist till references to it exists too, and select() seems to do just
this, keeping it in life, but is this guaranteed to continue working
in future releases of perl?

Please note that even if I can't imagine situations in which this
could be useful, but that described above, my question is *not* about
File::Find.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top