in reply to guidelines for inline coding

If you are looking for something like this, you can see that it works:
$ perl -e 'my $i = 0; print $i++, " " while $i < 10;' 0 1 2 3 4 5 6 7 8 9 ~ $ perl -e 'my @array = 1..10; print 2*$_, " " for @array;' 2 4 6 8 10 12 14 16 18 20 ~
you can also do things like this:
for (@array) { print $ERRORS "Error: value $_ is negative\n" and next if $_ < 0; push @valid_vals, $_; }
But you cannot do this:
5 statement if condition else other_statement;
You can't use an other clause when if is used as a statement modifier (i.e. after the statement).

Replies are listed 'Best First'.
Re^2: guidelines for inline coding
by Your Mother (Archbishop) on Sep 25, 2014 at 17:50 UTC

    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.

      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.
      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".