select()ing a lexical FH

Discussion in 'Perl Misc' started by Tassilo v. Parseval, Dec 18, 2003.

  1. 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Dec 18, 2003
    #1
    1. Advertising

  2. 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
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Dec 18, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    3
    Views:
    1,194
  2. Collin VanDyck
    Replies:
    0
    Views:
    340
    Collin VanDyck
    Nov 30, 2004
  3. Christopher Benson-Manica

    select()'ing on a Selector

    Christopher Benson-Manica, Jun 13, 2006, in forum: Java
    Replies:
    1
    Views:
    345
  4. palmiere
    Replies:
    1
    Views:
    473
    Erwin Moller
    Feb 9, 2004
  5. bernd
    Replies:
    2
    Views:
    689
    bernd
    Jan 26, 2012
Loading...

Share This Page