vaj has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm using IO::DIR as described in http://perldoc.perl.org/IO/Dir.html on Perl 5.8.4 and seeing a problem. I try to delete a file using the tied hash and it does not work, it returns an error code but no error text.

What am I doing wrong?

1. In the filesystem I run "touch foo", permissions are 644 or 666.

2. I run "delete.pl" as listed below.

3. The output is a blank line which means the delete failed and doing "echo $?" in the shell returns 255.

$ cat delete.pl #!/usr/bin/perl -w use IO::Dir; tie %dir, 'IO::Dir', '.'; delete($dir{'foo'}) || die "$!\n";

20050614 Janitored by Corion: Removed PRE tags, added formatting

Replies are listed 'Best First'.
Re: IO::DIR?
by BrowserUk (Patriarch) on Jun 14, 2005 at 05:46 UTC

    From the POD:

    Deleting an element from the hash will delete the corresponding file or subdirectory, provided that DIR_UNLINK is included in the OPTIONS.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      I tried it that way as well..
      
      ----
      
      $ cat delete.pl
      #!/usr/bin/perl -w
      
      use IO::Dir;
      
      tie %dir, 'IO::Dir', '.', DIR_UNLINK;
      
      delete($dir{'foo'}) || die "$!\n";
      
      ------
      
      and get the following warning..
      
      Argument "DIR_UNLINK" isn't numeric in bitwise and (&) at /usr/lib/perl/5.8/IO/Dir.pm line 94.
      
      The output is still the same as above and "echo $?" still returns 255. Have tried this on 3 separate systems.
      

        You need to import DIR_UNLINK

        use IO::Dir qw[DIR_UNLINK]; tie my %d, 'IO::Dir', '.', DIR_UNLINK;

        Or, as DIR_UNLINK is defined as the constant 1 you could just do

        use IO::Dir; tie my %d, 'IO::Dir', '.', 1;

        It's a strange way to define options!


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        You probably need to import the DIR_UNLINK, function from IO::Dir. I notice you aren't using strict, so it's probably interpreting the bareword "DIR_UNLINK" as a string.