in reply to Re^5: writing two files (different in length) to one output
in thread writing two files (different in length) to one output

The use of "precedence" instead of "list context" is better. Thanks for the clarification!

As to whether or not the comma operator is more readable in this situation, I leave that to the reader(s) to decide. My C background enables me to easily understand this construct, but many folks here may not find that "readable".

I think we are "drilling way down into the dirt here". The main point that is important here is testing of eof instead of !defined. I personally declare this thread a "success". Been "beat to death" and a definite, informative, high-performance, clear answer has been arrived at.

  • Comment on Re^6: writing two files (different in length) to one output

Replies are listed 'Best First'.
Re^7: writing two files (different in length) to one output
by LanX (Saint) on May 30, 2017 at 22:56 UTC
    I share your doubts about using comma in void context to separate commands like semicolon does, I only do it in exceptional cases.

    OTOH it's not too different of comma's behavior in list context

     print sqrt($i), $i, $i**2;

    just without returning the results.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Geez, ok, round #n

      The use of comma in your print statement works exactly the same.
      Consider:

      #!/usr/bin/perl use strict; use warnings; my $i = 3; # $i is not modified in this print statement: print sqrt($i), $i, $i**2; # 1.7320508075688839 ## 1.73205080756888 3 9 # $i is modified and new value of $i is used: print sqrt($i), --$i, $i**2; # 1.7320508075688824 ## 1.73205080756888 2 4
      I like the comma statement when it simplifies a loop, an example is a loop that prompts for an input:
      #!/usr/bin/perl use strict; use warnings; my $line; while ( (print "Prompt: "), $line=<STDIN>, $line !~ /^\s*Q(uit)\s*$/i +) { $line =~ s/^\s*//; $line =~ s/\s*$//; print"xyzzy \"$line\" and nothing happens\n"; } print "Some version of QUIT, QuIt, quit entered\n"; __END__ C:\Projects_Perl\testing>perl simplecommandline.pl Prompt: 123 xyzzy "123" and nothing happens Prompt: abc xyzzy "abc" and nothing happens Prompt: 123 abc xyzzy "123 abc" and nothing happens Prompt: qui xyzzy "qui" and nothing happens Prompt: quit doing that! xyzzy "quit doing that!" and nothing happens Prompt: QUiT Some version of QUIT, QuIt, quit entered
      Without the use of the comma statement in the while() statement, there has to be a "prompt" printed before the loop starts and a "prompt" must also be printed within the loop to "keep the loop going". The comma statement allows: a)ask for input, b)get input, c)test input all in one syntactically correct single statement. The "truthfulness" of a comma statement only rests upon the very last clause. This comma statement idea should be used sparingly. Very sparingly. I think we agree upon that.
        while ( (print "Prompt: "), $line=<STDIN>, $line !~ /^\s*Q(uit)\s*$/i )

        Having spent 5 minutes looking at this one line I'm still stumped as to the purpose of the capture group in the regex. Can you explain the purpose? ie. why would /^\s*Quit\s*$/i not perform exactly the same in your given code (since you have not subsequently referred to the captured data)? Thanks.

        Round #n+1 ;)

        As a side note, I think you can always use do {cmd;cmd } when you want to avoid cmd,cmd

        Update: nice trick with the while statement! :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!