Re: curly braces around a lexical file handle
by toolic (Bishop) on May 31, 2010 at 02:05 UTC
|
The curly braces indicate a block.
In your case, the curly braces are optional. But, in more complex cases, they are necessary. From the docs for print:
Note that if you're storing FILEHANDLEs in an array, or if you're using any other expression more complex than a scalar variable to retrieve it, you will have to use a block returning the filehandle value instead
| [reply] |
|
|
In your case, the curly braces are optional.
This is also a PBP-ism (#136). The general idea is always to do things in the same way even when unnecessary so no extra thought is needed (and no risk of forgetting is run) when it is necessary to do them in an unusual way.
E.g., if you always write
print {$filehandle} $some_data;
even when you don't have to, no extra thought is needed when it comes time to write
print {$hashref->{other}{handle}} $other_data;
instead.
Whether or not this approach is warranted in all cases is the subject of some debate.
| [reply] [d/l] [select] |
|
|
print( { $filehandle } $some_data );
Running into problems from omitting those parens is far more likely than running into problem from omitting those curlies. Furthermore, omitting the curlies around the file handle expression when it's needed is likely to result in a compile-time error, whereas omitting parens can fail silently and subtly.
If the argument you posted is valid, then it's far more important for
print $filehandle $some_data;
to be written as
print($filehandle $some_data);
than for it to be written as
print { $filehandle } $some_data;
| [reply] [d/l] [select] |
Re: curly braces around a lexical file handle
by ikegami (Patriarch) on May 31, 2010 at 03:55 UTC
|
A few functions take an expression in curlies as their first argument:
map ( { ... } ... )
grep ( { ... } ... )
print ( { ... } ... )
exec ( { ... } ... )
system ( { ... } ... )
In print's case,
- The expression in the curlies must result in the file handle to which print writes data.
- In a few special circumstances (including a plain scalar), the curlies around the expression can be omitted.
- The entire expression can be omitted in which case the data will be sent to the previously selected handle (normally STDOUT).
In other words,
print $write_fh "$database_query";
is a shortcut for
print { $write_fh } "$database_query";
| [reply] [d/l] [select] |
|
|
Interestingly, in all cases, either the block is optional, or there's an alternative, non-block syntax:
map EXPR, LIST;
grep EXPR, LIST;
print LIST; print BAREWORD LIST; print $HANDLE LIST;
exec STRING; exec LIST;
system STRING; system LIST;
And some you missed, also have a non-block syntax:
sort BLOCK LIST;
sort SUBNAME LIST; sort LIST;
eval BLOCK;
eval STRING;
Not sure whether there's a common issue behind this, or that it's just a coincidence. | [reply] [d/l] [select] |
|
|
You missed do (which has a non-block syntax as well).
| [reply] [d/l] |
|
|
Odd that the few special circumstances seem to be the vast majority of uses ;)
| [reply] |
|
|
I suspect that's exactly why they are special.
| [reply] |
Re: curly braces around a lexical file handle
by eyepopslikeamosquito (Archbishop) on May 31, 2010 at 02:40 UTC
|
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.
| [reply] |
|
|
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
| [reply] [d/l] [select] |
|
|
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
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
$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.
| [reply] [d/l] [select] |
|
|
|
|
|
|
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.
| [reply] [d/l] |
Re: curly braces around a lexical file handle
by graff (Chancellor) on May 31, 2010 at 18:17 UTC
|
I appreciate the clearly-stated question about print, and the very informative replies it inspired, but I had to wonder about this bit in the OP:
I'm modifying a program which uses IPC::Open3::open3 to fire up mysql and query the database.
I wonder if it might be easier to just use DBI (which, if told to connect to a mysql database, will automatically use DBD::mysql). | [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |