Skip to content

Say 'syntax error near "$"', not the following statement #22038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jidanni opened this issue Feb 27, 2024 · 16 comments
Closed

Say 'syntax error near "$"', not the following statement #22038

jidanni opened this issue Feb 27, 2024 · 16 comments
Labels
Closable? We might be able to close this ticket, but we need to check with the reporter

Comments

@jidanni
Copy link
Member

jidanni commented Feb 27, 2024

$ cat $i.pl
for ( 0 .. $ ) {

    #ho hum
    if ( $_ % $n[0] || $_ % $n[1] ) { next; }
    push @o, $_;
}
$ perl -c i.pl
syntax error at i.pl line 4, near "if"
i.pl had compilation errors.

Better would be

syntax error at i.pl line 1, near "$"

Else the user spends over five minutes trying to debug

    if ( $_ % $n[0] || $_ % $n[1] ) { next; }

(True story. In fact the earlier message was
syntax error ... near "next if", causing me to
see if changing next if ... to if () {next}
would help.

What I am trying to say is this time the wild
goose chase was so long that I should have
brought sandwiches.) perl/5.38.2

@haarg
Copy link
Contributor

haarg commented Feb 27, 2024

Until the if, the syntax is still valid. $ ) { is the start of accessing an element of the %) variable. For example:

$ perl -ce'
for ( 0 .. $ ) {
    #ho hum
    4 } ) {
    push @o, $_;
}'
-e syntax OK

So reporting the syntax error as the $ isn't really correct.

@jidanni
Copy link
Member Author

jidanni commented Feb 27, 2024

Well OK, but if it doesn't find what it was looking for, all the way to the end of the file even, then perhaps it should also mention the other case, that the user has simply forgotten something right after the $.

@iabyn
Copy link
Contributor

iabyn commented Feb 27, 2024 via email

@guest20
Copy link

guest20 commented Feb 27, 2024

The parser has to kind of guess what you actually meant, then go back and try to work out where the problem started.

Whether I shall turn out to be the hero of my own life, or whether that station will be held by anybody else, these pages must show. To begin my life with the beginning of my life, I record that I was born...

@jidanni
Copy link
Member Author

jidanni commented Feb 28, 2024

Maybe instead of guessing the wrong one sometimes, the parser could just mention both possibilities...

@Grinnz
Copy link
Contributor

Grinnz commented Feb 28, 2024

I think you are still underestimating the complexity. If the parser took the time to figure out every possible thing your incorrect syntax was meant to be, the error checking routine would be incredibly complex and would result in errors with easily 10 possibilities...

@jidanni
Copy link
Member Author

jidanni commented Feb 28, 2024

Well okay then. Yes I do not think we should haul in the AI. But still, the parser could say there is a problem between two points, instead of just mentioning one of the points.

In other words problem between a and b, instead of problem before b, or problem after a, or problem around b, or problem around a.

@guest20
Copy link

guest20 commented Feb 28, 2024

Hmm. A lot of perls errors are +/- a line. Usually -.

Maybe the parser could explain what it thought it was parsing at the time would help ... ... while parsing a key for %) starting at $i.pl line 1

@iabyn
Copy link
Contributor

iabyn commented Feb 28, 2024 via email

@iabyn
Copy link
Contributor

iabyn commented Feb 28, 2024 via email

@jidanni
Copy link
Member Author

jidanni commented Feb 28, 2024

I guess now we see the price for allowing $" $\ etc. in the language...
Must have seemed real cool at the time.
Too late to change now.
OK, feel free to close this report.

@guest20
Copy link

guest20 commented Feb 28, 2024

@iabyn it is parsing all those things, that's true.

maybe including that whole stack in the error could be too verbose... or too costly to implement

@xenu
Copy link
Member

xenu commented Feb 28, 2024

IMO the real problem here is that we allow whitespace where it doesn't make much sense.

@Tux
Copy link
Contributor

Tux commented Feb 28, 2024

IMO the real problem here is that we allow whitespace where it doesn't make much sense.

Whitespace use is one of the virtues in perl5. I know quite a bunch of people that have used liberal whitespace in interesting ways to express their way of thinking. What is logical to them (including me in them there) might be a complete mystery to others.

I personally would not write $h {key}, but it is allowed. I will always write foo ($arg) instead of foo($arg) or even worse foo( $arg ), but the fact that we can and that there are as many people that do makes the language rich and useful. Even if it does not make sense to some.

@khwilliamson
Copy link
Contributor

I think this is closable

@khwilliamson khwilliamson added the Closable? We might be able to close this ticket, but we need to check with the reporter label Apr 24, 2025
@jkeenan
Copy link
Contributor

jkeenan commented May 1, 2025

I think this is closable

Agreed; closing now.

@jkeenan jkeenan closed this as completed May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closable? We might be able to close this ticket, but we need to check with the reporter
Projects
None yet
Development

No branches or pull requests

9 participants