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

Hi! I'd like to delete a file and us "or warn" to write a warning message to a log file for any warnings that are incountered. If there are no warnings and the file is deleted successfully, then I'd like to print a "successfully deleted" message to a log file. The code below will print the "successfully deleted" message everytime, how can I code it so that it only prints the "successfully deleted" message to the log file when there are no warnings? Thank you to any who can help me out.

$ftp->delete($file) or warn print LOG "Cannot delete $file - ", $ftp- +>message; print LOG "$file successfully deleted\n";

Replies are listed 'Best First'.
Re: or warn question
by sauoq (Abbot) on Jun 08, 2012 at 18:47 UTC

    The do_something or warn_or_die($message); idiom is a good one. But there's no requirement that you use it when it doesn't fit well. Just do the obvious...

    if ($ftp->delete($file)) { print LOG "$file successfully deleted\n"; } else { print LOG "Cannot delete $file - ", $ftp->message; }

    -sauoq
    "My two cents aren't worth a dime.";
      This one worked like a charm too. Thank you for taking the time to reply!
Re: or warn question
by BrowserUk (Patriarch) on Jun 08, 2012 at 19:35 UTC

    Since you want to log a message regardless, with only the content of the message determined conditionally, the ternary seems ideal suited:

    print LOG $ftp->delete($file) ? "$file successfully deleted\n" : "Cannot delete $file - " . $ftp->message;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: or warn question
by clueless newbie (Curate) on Jun 08, 2012 at 18:28 UTC
    Something like
    $ftp->delete($file) and do { warn "$file successfully deleted\n" } or +do {warn print LOG "Cannot delete $file - ", $ftp-message };
    perhaps? Please change "warn print LOG" to "warn"!
      This did work, thank you for your time!
Re: or warn question
by AnomalousMonk (Archbishop) on Jun 08, 2012 at 22:17 UTC

    All a statement like
        warn print WHATEVER 'some message';
    will do insofar as generating a warning message is to print to standard error the return value of the print function, a 1 in the case of a successful print. Is this of any conceivable use?

    c:\@Work\Perl>perl -wMstrict 2> two 1> one -le "warn print 'is this a warning?'; " c:\@Work\Perl>type one is this a warning? c:\@Work\Perl>type two 1 at -e line 1.
Re: or warn question
by zentara (Cardinal) on Jun 08, 2012 at 18:52 UTC
    I havn't tested it, but possibly check for $!

    #untested: $ftp->delete($file); if( $! ) { warn print LOG "Cannot delete $file - $!", $ftp->message;} else { print LOG "$file successfully deleted\n";}

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      How do you know the delete() method sets $!?

      I didn't test this either, but thank you for your reply!
Re: or warn question
by druthb (Beadle) on Jun 09, 2012 at 13:00 UTC

    Peripherally related to this is autodie...for many I/O operations, it can replace the do_something_or_die construct. In my work, I massage a whole bunch of little files, and so autodie really saves me a ton of typing!

    use Modern::Perl; use autodie; open my $fh,'>','filename.dat'; #will die() if it can't. say "Okay, the file's open, now let's do something.";

    This may or may not be useful in the OPs issue, since there's apparently an FTP service going on that is black-boxy, from our perspective.

    D Ruth Bavousett