Skip to content
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

when {} is a syntax error #60

Closed
schwern opened this issue Oct 28, 2012 · 7 comments
Closed

when {} is a syntax error #60

schwern opened this issue Oct 28, 2012 · 7 comments
Assignees
Labels

Comments

@schwern
Copy link
Contributor

schwern commented Oct 28, 2012

This works fine.

func foo( $thing = [1,2,3] when [] ) {
    print @$thing
}

This is a messy parse error.

func foo( $thing = { foo => 23 } when {} ) {
    print keys %$thing;
}

Not enough arguments for grep at /Users/schwern/tmp/test.plx line 3, near "} $arg"
syntax error at /Users/schwern/tmp/test.plx line 3, near "} $arg"
syntax error at /Users/schwern/tmp/test.plx line 5, near "}"
Execution of /Users/schwern/tmp/test.plx aborted due to compilation errors.

Something's gone wrong with the injection.

my $thing = !(@_ > 0) ? ( { foo => 23 } ) : do{ no warnings; my $arg = $_[0]; (grep  {} $arg) ? ( { foo => 23 } ) : $arg}; Method::Signatures->too_many_args_error(1) if @_ > 1; 
@schwern
Copy link
Contributor Author

schwern commented Oct 28, 2012

OIC it thinks its a block. Patch on the way.

@ghost ghost assigned schwern Oct 28, 2012
@schwern
Copy link
Contributor Author

schwern commented Oct 28, 2012

Hrm. I can special case an empty hash, but what about hashes in general? I don't think there will be much call for checking for anything but an empty hash, so I'm inclined to just special case it. Anything more complicated can be done as a full block.

schwern added a commit that referenced this issue Oct 28, 2012
Also added a test for `when []`.  I didn't find a good file for
testing when parsing issues, so I made one up.

For #60
@thoughtstream
Copy link
Contributor

Hashes in general are already supported.

PPI uses the usual perlish guesswork to determine
whether a pair of curlies are a hash or a block.

Empty curlies are arguably a useless special case
as an empty hash will never smartmatch anything.

Of course, treating {} as an empty block would be
equally useless, so special casing them as empty
hash is probably for the best (i.e. least surprising).

@barefootcoder
Copy link
Contributor

Well, we should probably special-case it as whatever-doesn't-generate-a-syntax-error. :-D

This fix will go into the next dev release. Just waiting now for CPANTesters to throw up some reports for me to look at ...

@schwern
Copy link
Contributor Author

schwern commented Oct 29, 2012

@thoughtstream PPI might have that guesswork, but inject_for_sig() isn't using it. It's using a regex. I'll look at switching it to PPI, but this fix to the existing regex should go in.

And an empty hash ref smart matches, surprise, an empty hash ref.

perl -wle '$h = {};  print $h ~~ {} ? "Match" : "No Match" '
Match

perl -wle '$h = { foo => 23 };  print $h ~~ {} ? "Match" : "No Match" '
No Match

This is handy for providing a default if the user passes in an empty hash ref, similar to using when "" for a string parameter.

@thoughtstream
Copy link
Contributor

Very good point re empty hashref matching empty hashes.
This definitely should be special-cased.

@barefootcoder
Copy link
Contributor

Fixed in version 20121201.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants