proxy-man has asked for the wisdom of the Perl Monks concerning the following question:

I am just curious, do we have a way to intercept an exception during glob execution? Say, a directory does not belong to our permissions or something like that. My answer is - NO, glob does not allow us to do this. What is about your answer? Thanks a lot.

Replies are listed 'Best First'.
Re: perl glob and an exception
by Corion (Patriarch) on Sep 25, 2015 at 09:25 UTC

    There is no need for glob to allow that. The standard mechanism is eval to catch exceptions in Perl.

      eval can catch runtime error, it does not work for a case below:

      user@host: mkdir ~/test

      user@host: touch ~/test/file1.txt

      user@host: chmod 000 ~/test

      ### perl code my $dir = '/home/user/test'; my @files = eval { glob("$dir/*.txt") }; print "$@" if $@; # here is no runtime error print Dumper(\@files); # shows an empty list

        So, what is your question? If there is no exception, you can't catch it either.

        Maybe you wanted to ask how glob can tell you whether there was a problem with one of the filespecs?

        For that, you will have to use opendir and readdir instead, and check for errors yourself.

Re: perl glob and an exception
by Anonymous Monk on Sep 25, 2015 at 09:27 UTC