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

Hi I'm using the module File::Find::Name. I'd like to do a special handling in the case my destination folder doesn't exists. I'm using the -w option in perl and also get the warning on the console. How can i access the warning as a variable to react according to the content? e.g.:
@files = FileHelper::getFileList($dstPath);
In case the dst is correct and there are files, i return the names as an array. If there are no files i get an empty array and in case the dst folder doesn't exist a warning will be written to the console (C'ant stat .. No such file or directory). I'd like put the warning into a variable. How can i do this? Thanks in advance Michael

Replies are listed 'Best First'.
Re: store a warning in a variable
by PeterPeiGuo (Hermit) on Nov 09, 2010 at 14:35 UTC

    A better approach is to check whether the directory exists first, for example use -d.

    Don't handle known conditions as "exceptional" ones.

    Peter (Guo) Pei

      First of all: Thanks to all replies. It helped me very much!

      Usually i would agree with Peter to check first, if the folder exists. In my case the folder is a mounted share folder from a different server. It should be mounted all the time. I try to handle the exception if this mount doesn't work.
      - normal case: files to copy found or no files found
      - exception: directory NOT found

      cheers Michael
Re: store a warning in a variable
by JavaFan (Canon) on Nov 09, 2010 at 13:03 UTC
    my $warning; $SIG {__WARN__} = sub {$warning = @_ == 1 ? shift : join " ", @_};
      How can the length of @_ be anything other than 1?
        You are right. It even becomes much simpler:
        my $warning; $SIG{__WARN__} = sub {$warning = shift};