Are you asking about the parens around the arguments to my? They don't do anything relating to context. They don't do anything at all.
The parens around my's argument only have a special effect when the my is the LHS operand of an assignment operator. They cause the assignment operator to be a list assignment operator instead of the scalar assignment operator, as described in Mini-Tutorial: Scalar vs List Assignment Operator.
| [reply] [d/l] [select] |
There is no list context, the first argument to open is always a scalar:
$ perl -le'print prototype "CORE::open"'
*;$@
| [reply] [d/l] |
$ perl -we "$file='X'; open my $fh, $file;"
Parentheses missing around "my" list at -e line 1.
$ perl -we "$file='X'; open my($fh), $file;"
Once you get in the habbit of adding parens, you keep adding them (I know I do), even if you switch to 3 argument open which doesn't trigger the bug
$ perl -we "$file='X'; open my $fh, '<', $file;"
| [reply] [d/l] [select] |
This is the correct answer. I do it out of habit without even thinking about it because of this old issue
--brian d foy, who hasn't been able to login in ages since the password resetter thingy doesn't work.
| [reply] |
Thanks for replies, I almost suspected the parens didn't mean list context here. Any reason why they're used like this in the book? It frequently recommends the use of less punctuation.
| [reply] |
open my $fh, '<', $file
would mean
open(my($fh, '<', $file))
Maybe the author didn't like to rely on the specialness of my?? I'm really reaching here.
| [reply] [d/l] [select] |
You guys sure write weird open calls. I keep thinking you’re making an easy thing hard.
See how easy that is? Why make things so hard on yourselves? Always use parens on you function calls, and you will never go astray. So simple it’s impossible to forget. And no more precedence problems.
--tom
| [reply] [d/l] [select] |
| [reply] |
Opinions Considered Harmful
- Cᴀᴠᴇᴀᴛ Lᴇᴄᴛᴏʀ:
- If by chance you’re one to be easily bothered by strong opinions, especially those against which you have an irrational, emotional reaction, please do us all a favor and skip reading this posting. 🙈 🙉 🙊
On Monday, 13 June 2011 at 10:28 ㏂ ᴍᴅᴛ,
chromatic
in quoting yours truly has done me the honor to write: › Always use parens on you[r] function calls…
I’ve given that advice too, but what makes open a function call and die not?
Quite so! Rem acū tetigistī!
Not merely a just question, it would also seem a perfectly straightfoward question as well. The operative term there is seem, as your seemingly simple question comes accompanied by seriously subobvious traps to trouble both the unwary and the hypervigilant alike.
To get a feel for how my current Perl coding style lays out on the page, including in particular my use of parentheses on function calls, I encourage you to look through my recent work in my evergrowing Unicode Tool Chest. Whatever style that that code evinces, and I am absolutely certain that it somehow does so, it is not something I have ever managed to explain coherently, and least not well enough to get something like perltidy to come close to reproducing. Nevertheless, I do believe that taken as a whole, my personal style is consistent, predictable, readable, and pedagogically useful. I should’t be surprised
if it were found to be so idiosyncratically mine that it would prove readily identifiable as tchrist–code by one of those clever machine‐learning program trying to identify plagiarism.
The fiftyish programs in my Unicode Tool Chest are often a lot more useful, or can be used in many clever ways other than are immediately apparent. Some of those that I use daily or more include:
There are around 50 programs, modules, and libraries in that directory, almost all having to do with Unicode. Some are one‐shots, some are well‐documented standard tools I use daily, and all I believe have something to say about Perl (and sometimes C and sometimes Java) programming. Some delightful little simpletons are genuine lifesavers, but you’d never know it without playing around with them a bit. And some are just downright hilarious in the extreme. You’ll see.
ℹ⃝ Ask me, Ask me, Ask me!
Lastly, I’m happy to entertain questions both public and private about the code.
| [reply] [d/l] [select] |