in reply to curly braces around a lexical file handle

This style is recommended in Perl Best Practices, Chapter 10 (I/O), p. 217, Printing to Filehandles: "Always put filehandles in braces within any print statement".

The idea is that putting braces around the filehandle helps it stand out clearly and clarifies your intentions in that you really did intend it to be treated as a filehandle and didn't just forget a comma.

  • Comment on Re: curly braces around a lexical file handle

Replies are listed 'Best First'.
Re^2: curly braces around a lexical file handle
by moritz (Cardinal) on May 31, 2010 at 07:44 UTC
    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

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

Re^2: curly braces around a lexical file handle
by ikegami (Patriarch) on May 31, 2010 at 04:01 UTC

    If you pass a file handle to print, the default assumption is that you want to print to the file handle. Adding curlies doesn't change anything.

    The reasoning you posted only makes sense if one assumes the file handle is badly named. The fix for a bad variable name is not to add silly curlies to print, it's to change the variable's name.