Skip to content

Commit

Permalink
Added trap about the (ab)use of $/
Browse files Browse the repository at this point in the history
Hope this closes #1853. If it does not, or it should be moved
somewhere else, feel free to re-open or to suggest something.
  • Loading branch information
JJ committed Mar 20, 2018
1 parent ca78a22 commit 42165a4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions doc/Language/traps.pod6
Expand Up @@ -1396,6 +1396,31 @@ your C<LEAVE> block is defined:
LEAVE say .Int with $x
=end code
=head1 Grammars
=head2 Using regexes within grammar's actions.
=begin code :skip-test
grammar will-fail {
token TOP {^ <word> $}
token word { \w+ }
}
class will-fail-actions {
method TOP ($/) { my $foo = ~$/; say $foo ~~ /foo/; }
}
=end code
Will fail with C<Cannot assign to a readonly variable ($/) or a value>
on method C<TOP>. The problem here is that regular expressions also
affect C<$/>. Since it is in C<TOP>'s signature, it is a read-only
variable, which is what produces the error. You can safely either use
another variable in the signature or add `is copy`, this way:
=begin code
method TOP ($/ is copy) { my $foo = ~$/; my $v = $foo ~~ /foo/; }
=end code
=head1 Unfortunate generalization
=head2 C<:exists> with more than one key
Expand Down

1 comment on commit 42165a4

@drforr
Copy link

@drforr drforr commented on 42165a4 Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks great, especially the hint on 'is copy'. Thanks, just trying to help future authors.

Please sign in to comment.