Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: bsd_glob does not reset $! (The lessons learned)

by stefan k (Curate)
on Sep 29, 2006 at 09:31 UTC ( #575488=note: print w/ replies, xml ) Need Help??


in reply to bsd_glob does not reset $!

Hi,
thanks to the help from the CB I have collected some answers.

  • I am still confused about the way $! behaves in this case, but I am not supposed to understand it (unless I learn the internals of Perl) and I am only supposed to use it when any error was signaled.
  • The desired outcome of bsd_glob can be achieved by passing the flag !GLOB_NOCHECK. Then the returned list will not contain the search pattern upon unsuccessful globbing. In my case this was hard because the real-world application in question sometimes uses a full pathname as a search pattern.
  • For performance and sanity my routine will first do a test for file-existance and then try to glob it. It feels clumsy, though.
  • Another way to check the outcome would be to undef $! before calling bsd_glob but since the documentation does not explicitly state the observed behaviour I will not rely on it to work.
I think this sums it up.

Thanks

Regards... stefan k
you begin bashing the string with a +42 regexp of confusion


Comment on Re: bsd_glob does not reset $! (The lessons learned)
Select or Download Code
Re^2: bsd_glob does not reset $! (The lessons learned)
by ikegami (Pope) on Sep 29, 2006 at 15:33 UTC

    For performance and sanity my routine will first do a test for file-existance and then try to glob it. It feels clumsy, though.

    You need to check for file-existance afterwards, not beforehand. glob is not guaranteed to produce a list of existing files. For example, glob('file{a,b}') will return filea and fileb whether either, neither or both exists.

    @list = bsd_glob('~gnat', GLOB_ERR); die("Unable to glob: $!\n") if GLOB_ERROR; # Keep only references to existing files. @list = grep -f, @list; die("glob returned no existing results\n") if not @list;

    I am still confused about the way $! behaves in this case

    This case is not special.

    # XXX WRONG $rv = print $fh (...); if ($!) { die("Unable to write to file: $!\n") } # Ok $rv = print $fh (...); if ($rv) { die("Unable to write to file: $!\n") }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://575488]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-12-21 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (107 votes), past polls