Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Unexpected parser error

by saintmike (Vicar)
on Feb 22, 2007 at 06:14 UTC ( [id://601488]=perlquestion: print w/replies, xml ) Need Help??

saintmike has asked for the wisdom of the Perl Monks concerning the following question:

Who would have thunk that perl's parser would stumble over something as clear and simple as
sub function { return 8 } my $a = function / 4;
which is pretty much as clear and simple as it gets? The result is a syntax error:
Search pattern not terminated at ./test.pl line 10.
Sure, function can be disambiguated to function(), but there is no real ambiguity in the first place, or is there?

Replies are listed 'Best First'.
Re: Unexpected parser error
by ikegami (Patriarch) on Feb 22, 2007 at 06:30 UTC

    but there is no real ambiguity in the first place, or is there?

    Us humans can see the picture and deduce an intent to divide in this example, but it's not easy to write a program to do that. Perl decides when it sees the / whether the / is part of the argument list. Since / can start an argument list, Perl guesses it's part of the argument list. When it detects an error, Perl has no way of knowing if it's because the programmer screwed up the match operator or if it's because it guessed wrong. Of course, you could tell Perl the function takes no arguments.

    sub func() { return 8 } print func / 4; # 2

    + and - are also tricky tokens.

    sub foo { return 8 } print foo + 4; # 8 print foo(+4); # 8 print foo() + 4; # 12 sub bar() { return 8 } print bar + 4; # 12 print bar() + 4; # 12

    There are problems with using prototypes, however. You should look into that before using them.

    As a programmer, you should know that omitting the parens leads to this type of problem and consider missing parens as the source of problems.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Unexpected parser error
by diotalevi (Canon) on Feb 22, 2007 at 06:17 UTC

    You forgot to put the parentheses on the end of function(). Perl started parsing function( / 4; and failed when it ran out of file while parsing the regular expression  4;. You could also have used a prototype on function but those are pretty much always a bad idea.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      You forgot to put the parentheses on the end of function().
      No, I didn't.

      That's why I wrote "Sure, function can be disambiguated to function()".

      My point: If 'function' is known as a function name at this point, perl should be smart enough to throw out the possibility that "function / 4" is a regular expression because that's an insane assumption.

        Ok, given that you know perl knows that function is variadic, you know it's going to try to put everything following it into its parameter list. My understanding is that the parser is only able to look ahead one token at a time. The fact that it got two tokens ahead before it found out it couldn't parse things means it just couldn't continue. It isn't capable of backing up from function( / 4; to function() / 4;. You'll just have to provide that information to perl soon enough for the parser to use it. Sorry. Either use the parameter list or set the prototype.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Re prototypes, specifically:
      sub function() { 8 } my $a = f / 4;

        I consider sharing that bit of information a disservice. Prototypes in perl are something to arrive at far into a person's perl experience and generally to skip by. They're good to know about but should almost never be used.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Unexpected parser error
by PreferredUserName (Pilgrim) on Feb 22, 2007 at 21:24 UTC
    I agree with the original poster that it's weak for Perl to accept function * 4, but not function / 4, when multiplication and division should behave identically.

    Making parens optional is a mistake in my view, since it obscures what's really going on.

    And who wants an obscurantist Perl? ;)

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found