in reply to Re: guidelines for inline coding
in thread guidelines for inline coding

I like the additions. I feel like print "xyz" and… is a Heisenbug waiting to happen though. As rare and weird as it is, print can return false. Can’t remember who but some monk was so concerned about it they advocated checking its return on each call.

Replies are listed 'Best First'.
Re^3: guidelines for inline coding
by Laurent_R (Canon) on Sep 25, 2014 at 21:41 UTC
    Hi Your Mother,

    I agree that all my three examples are somewhat simplistic, I just wanted to convey the idea of the syntax that the OP was apparently looking for. In the third case, it is true that if print fails, the next statement will not be executed and the next line of code will be executed, which is presumably not what you want. But, OTOH, if the print statement fails, we have much deeper trouble than this possible Heisenbug, so that the random bug is really not the major concern.

    Having said that, I fully agree that the do_some_statement() and do_some_other_statement() if something_is_true is nice for writing concise code, but can be dangerous if you are not cautious enough.

    I once made the mistake of writing some code basically looking like that:

    $nb_errors++ and next if some_condition(); # wrong, don't do it
    That worked perfectly for most of the cases, except the first time when an error occurs through the loop, because $nb_errors was false (either 0 or undef) the first time through the loop. Changing it to:
    ++$nb_errors and next if some_condition();
    is sufficient to correct the issue. But it shows that such a construct can be dangerous and must be carefully considered.
Re^3: guidelines for inline coding
by BrowserUk (Patriarch) on Sep 25, 2014 at 19:29 UTC
    As rare and weird as it is, print can return false. Can’t remember who but some monk was so concerned about it they advocated checking its return on each call.

    Completely pointless. What are you going to do if it returns false? print an error 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.
      What are you going to do if it returns false?
      use strict; use File::Temp 'tempfile'; my $fh = tempfile(UNLINK=>1); select($fh); print "foo" or warn "print 1 fail"; close $fh; print "bar" or warn "print 2 fail"; __END__ print 2 fail at - line 8.

      ... or I could die, print somewhere else like a log, etc.

      While of course I still wouldn't check every print call in a normal program, I take your post to be implying that there's nothing one can do and any checking of the return value is "Completely pointless".

        And what if warn also fails? Ditto die? Shouldn't you be checking those also?

        Both quite likely if the cause of the print (to stdout) is console corruption or failure; or memory exhaustion; or device failure; or ...


        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.