in reply to Re: curly braces around a lexical file handle
in thread curly braces around a lexical file handle

IMHO it's not just about forgetting the comma. When you write it with or without comma, two very different things look very similar.

The additional curlies add "visual weight", one sees immediately that

print $a, $b; # and print { $a } $b;

do different things.

I don't usually use the curlies, but that's more a result from laziness and thoughtlessness than anything else.

I kinda like the Perl 6 way of doing it:

print $a, $b; # print both variables $a.print: $b; # treat $a as a file handle

Replies are listed 'Best First'.
Re^3: curly braces around a lexical file handle
by ikegami (Patriarch) on May 31, 2010 at 16:49 UTC

    That's not a realistic example. If you were printing two variables, it would look like

    print $name, ", ", age;
    or
    print "$name, age";

    And neither looks similar to the following to me:

    print $fh $age;

    As for your Perl6 eaxmple, you can do the same in Perl5.

    $fh->print($s); # Perl5 $fh.print: $s; # Perl6

      That might be true for debugging output, put otherwise usage of print is pretty diverse.

      Ack'ing through my private Perl code I've found pretty many occurrences of print $variable, EXPRESSION and print $variable EXPRESSION where EXPRESSION didn't start with a quote that looked like a separator.

      Perl 6 - links to (nearly) everything that is Perl 6.

        Again, print $variable EXPRESSION is not realistic code.

        I'm saying the real problem is bad naming conventions. You can't argue against that by altering the name of the variables you used. (I'm assuming your didn't actually use "$variable" as the name of your file handle.) What's your actual code?

        Update: Clarified.

Re^3: curly braces around a lexical file handle
by JavaFan (Canon) on May 31, 2010 at 10:38 UTC
    $a.print: $b;   # treat $a as a file handle
    Good thing you added the comment. I would have taken $a.print to mean the value of $a is to be printed out -- probably to handle $b.
      Well, it's new syntax, so it's no surprise you need to learn somthing about it.

      It becomes clearer if you think more about: A method call (and $a.print: $b is a method call) can have one invocant, but many ordinary arguments. Which one is special, the invocant or the first, ordinary argument?

      When you see

      $a.print: $b, $c, $d, $d;

      Do you still think that $a is printed to file handle $b? Or to all of the arguments, all treated as file handles?

      That would be a very bad huffman coding...

        That would be a very bad huffman coding...

        Only if you think $o.print: $fh; precludes $fh.print: $o; from existing. There's no reason both can't exist. That said, I would call the former "dump" rather than "print".

        $o.dump; $o.dump: $fh;