Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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 ./ 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).

Replies are listed 'Best First'.
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: 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.


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


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.


        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://917108]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-02-20 04:34 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (267 votes). Check out past polls.