Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Parentheses missing around "my" list

by rastoboy (Monk)
on Jul 27, 2011 at 19:16 UTC ( #917108=perlquestion: print w/ replies, xml ) Need Help??
rastoboy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Brothers!

I have a long time issue, off and on, of receiving this error. Inevitably, it's when I'm opening a lexical file handle like this:

open my $file, $filename;
And I mean that literally, as in I use a scalar that contains the filename. Sometimes--but not usually--I get an error like:
Parentheses missing around "my" list at ./protcpdump.pl line 40.
I can "fix" this by merely putting the filename scalar in quotes:
open my $file, "$filename";
I know I'm missing something, but for the life of me I can't find the common denominator in the situations where this happens. What am I missing?

Any input would be greatly appreciated! (I hate not really understanding errors like this).

Comment on Parentheses missing around "my" list
Select or Download Code
Re: Parentheses missing around "my" list
by davido (Archbishop) on Jul 27, 2011 at 19:39 UTC

    It's an inconsistency in Perl's implementation of that warning resulting from how Perl decides whether it's looking at a list or not. Strictly speaking the code is ambiguous; do you want my $this, $that, or open my ( $fh ), $filename? You know the answer, and Perl runs it right, but the warning is generated. It's documented here: https://rt.perl.org/rt3/Public/Bug/Display.html?id=7250. It's been around for awhile. You can always wrap parens around the my declaration, like this:

    open my ( $fh ), $filename;

    It sometimes (maybe always) goes away if you put "or die $!;" after your open call, so it was less apparent before the days of autodie.


    Dave

Re: Parentheses missing around "my" list
by Tanktalus (Canon) on Jul 27, 2011 at 19:41 UTC

    The compiler (lexer/parser) is trying to figure out whether this is my VAR, or my LIST, and is getting confused. When you put in the quotes, perl can figure it out.

    Instead, you should switch to the three-arg version of open, and make your mode explicit. It's a good habit, IMO, because the maintenance programmers will love you for it. :-) open my $file, '<', $filename or die "Can't open $filename: $!";

Re: Parentheses missing around "my" list
by ikegami (Pope) on Jul 27, 2011 at 19:43 UTC
    f g EXPR, EXPR

    is the same as

    f(g(EXPR, EXPR))

    my is very special in the sense that it won't gobble arguments that wouldn't be valid for it, which is why adding the quotes help.

    This resolves the ambiguity:

    open my ($file), $filename

    This is probably what you actually want:

    open my $file, '<', $filename
Re: Parentheses missing around "my" list
by a (Friar) on Jul 27, 2011 at 19:56 UTC
    Yeah, my guess is that open is looking at the the 'my' stuff in list context, that is, open takes 1, 2, 3 or more arguments and you can put an array var in the first position - so my is warning you that you've got an un-paren-ed list of vars because open's trying to get it to handle the rest of the list. "my" is a uni-valued function (so you need parens/list for multiple values). If you use the 3 element form (your example has the var names backwards or, misleading, anway) though:
    open my $filehandle, ">", $file
    or add the 'or die ... ' test
    open my $filehandle, $file or die "can't open $file: $!";
    (both recommended best practices for "open") you don't get the complaint. Yeah, not really the clear-cut answer you were looking for though ... hmm, may just be a parser corner case:
    $ perl -e 'use warnings; use strict; my $filename = "/tmp/hey"; open m +y $file, $filename' $ perl -e 'use warnings; use strict; my $filename = "/tmp/hey"; open m +y $file, $filename;' Parentheses missing around "my" list at -e line 1.
    only diff is the final semicolon before the close quote

    a

Re: Parentheses missing around "my" list
by JavaFan (Canon) on Jul 27, 2011 at 20:08 UTC
    Some other ways beside the already mentioned to avoid the error:
    open my $file, +$filename; open my $file => $filename; open my $file, do {$filename}; my $file; open $file, $filename;
      open my $file, do {$filename};

      I'm glad I had a mild lunch.


      Dave

        That is totally awesome, y'all, thanks!

        As usual, it turns out that shortcuts I take during "development" are more trouble than their worth. Leaving off error handling for opening the file, not using the three argument version of open--both things I know perfectly well are best practice.

        Still, very educational, thanks again!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://917108]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-20 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls