dir is open

Discussion in 'Perl Misc' started by George Mpouras, Nov 1, 2013.

  1. how do I know if a directory is open so not have warnings when closedir
    it ? At files there is the fileno.
    George Mpouras, Nov 1, 2013
    #1
    1. Advertising

  2. George Mpouras <> wrote:
    >how do I know if a directory is open so not have warnings when closedir
    > it ? At files there is the fileno.


    By checking the return value of opendir()?

    jue
    Jürgen Exner, Nov 2, 2013
    #2
    1. Advertising

  3. George Mpouras

    $Bill Guest

    On 11/1/2013 15:05, George Mpouras wrote:
    > how do I know if a directory is open so not have warnings when closedir it ? At files there is the fileno.


    Something to this effect should work:

    my $dh;
    opendir $dh, $_ or do { warn "open $_ failed: $! ($^E)\n"; undef $dh; };

    ....
    readdir $dh, ...
    ....


    closedir $dh if $defined $dh;
    $Bill, Nov 2, 2013
    #3
  4. Στις 2/11/2013 7:37 πμ, ο/η $Bill έγÏαψε:
    > y $dh;
    > opendir $dh, $_ or do { warn "open $_ failed: $! ($^E)\n"; undef $dh; };
    >
    > ...
    > readdir $dh, ...
    > ...
    >
    >
    > closedir $dh if $defined $dh;





    this is a very good idea.

    I wonder where is this Perl mystic cryptic where the handles are kept.
    fileno knows it , but dirhandles are not there , but there must be also
    one for the dirhandles
    George Mpouras, Nov 2, 2013
    #4
  5. George Mpouras <> writes:
    > Στις 2/11/2013 7:37 πμ, ο/η $Bill έγÏαψε:
    >> y $dh;
    >> opendir $dh, $_ or do { warn "open $_ failed: $! ($^E)\n"; undef $dh; };
    >>
    >> ...
    >> readdir $dh, ...
    >> ...
    >>
    >>
    >> closedir $dh if $defined $dh;

    >
    >
    >
    >
    > this is a very good idea.


    To a degree: perl knows if the directory handle is open and will close
    it automatically when the corresponding object is destroyed. This means
    this will work:

    my $dh;
    opendir($dh, '/tmp') if rand(10) > 5;
    $dh = undef;

    or this

    {
    my $dh;
    # do something which might open a dir handle
    }
    # will be closed here if it was opened
    Rainer Weikusat, Nov 2, 2013
    #5
  6. George Mpouras

    $Bill Guest

    On 11/2/2013 07:38, Rainer Weikusat wrote:
    >
    > To a degree: perl knows if the directory handle is open and will close
    > it automatically when the corresponding object is destroyed. This means
    > this will work:
    >
    > my $dh;
    > opendir($dh, '/tmp') if rand(10) > 5;
    > $dh = undef;


    But obviously, you can't do the undef until after the readdir/closedir
    is done or you get no dir entries.

    > or this
    >
    > {
    > my $dh;
    > # do something which might open a dir handle
    > }
    > # will be closed here if it was opened
    >
    $Bill, Nov 2, 2013
    #6
  7. On 11/2/2013 6:07 AM, George Mpouras wrote:
    > Στις 2/11/2013 7:37 πμ, ο/η $Bill έγÏαψε:
    >> y $dh;
    >> opendir $dh, $_ or do { warn "open $_ failed: $! ($^E)\n"; undef $dh; };
    >>
    >> ...
    >> readdir $dh, ...
    >> ...
    >> closedir $dh if $defined $dh;

    >
    > this is a very good idea.
    >
    > I wonder where is this Perl mystic cryptic where the handles are kept.
    > fileno knows it , but dirhandles are not there , but there must be also
    > one for the dirhandles


    Yes, see Ben's response.

    So perhaps another slightly "better" hack:

    closedir $dh if ref $dh eq 'GLOB';

    --
    Charles DeRykus
    Charles DeRykus, Nov 2, 2013
    #7
  8. On 11/2/2013 3:57 PM, Ben Morrow wrote:
    >
    > Quoth Charles DeRykus <>:
    >> On 11/2/2013 6:07 AM, George Mpouras wrote:

    > ...
    >>>> closedir $dh if $defined $dh;
    >>>
    >>> this is a very good idea.
    >>>

    >> ...
    >>
    >> So perhaps another slightly "better" hack:
    >>
    >> closedir $dh if ref $dh eq 'GLOB';

    >
    > I don't think this helps, does it? You still need eval if you want to
    > pass in arbitrary scalars.
    >


    Indeed, it looked like "eval" was being thoroughly ignored for a
    "defined $dh" so I suggested the slight improvement.

    --
    Charles DeRykus
    Charles DeRykus, Nov 3, 2013
    #8
    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. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    413
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Matthew Denner
    Replies:
    1
    Views:
    161
  3. Kga Agk
    Replies:
    2
    Views:
    140
    Kga Agk
    Jun 29, 2009
  4. Nick Gnedin
    Replies:
    2
    Views:
    148
  5. MRAB
    Replies:
    0
    Views:
    112
Loading...

Share This Page