Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all Perl 6 quoting constructs have term precedence?

The < > has term precedence. Here's the example from the docs:

say <a b c>[1];

I figured the same precedence would apply to all of the quoting operators. This works:

my $string = '5+8i';
my $number = <<$string>>;
say $number;

This interpolates $string and creates allomorphes (in this case a ComplexStr):

(5+8i)

But, if I try to index it like the example from the docs, it doesn't compile:

my $string = '5+8i';
my $number = <<$string>>[0];
say $number;

I'm not quite sure what Perl 6 thinks is happening here. Perhaps it's thinking it's a hyperoperator:

===SORRY!=== Error while compiling ...
Cannot use variable $number in declaration to initialize itself
at /Users/brian/Desktop/scratch.pl:6
------>     say $⏏number;
    expecting any of:
        statement end
        statement modifier
        statement modifier loop
    term

I can skip the variable:

my $string = '5+8i';
say <<$string>>[0];

But that's a different error that can't find the closing quotes:

===SORRY!=== Error while compiling ...
Unable to parse expression in shell-quote words; couldn't find final '>>'
at /Users/brian/Desktop/scratch.pl:8
------> <BOL>⏏<EOL>
    expecting any of:
        statement end
        statement modifier
        statement modifier loop
like image 722
brian d foy Avatar asked Jul 02 '17 15:07

brian d foy


People also ask

Which operator has the highest precedence in Perl?

A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses, and any function whose arguments are parenthesized. Actually, there aren't really functions in this sense, just list operators and unary operators behaving as functions because you put parentheses around the arguments.

What's new in Perl 6?

Perl 6 instead provides opaque objects by default, with language support for creating classes and instances and declaring class and instance attributes. It also provides multiple ways to customize class and object behavior, from instantiation to destruction.

Why do people still use Perl 5?

Innumerable programmers, hackers, system administrators, hobbyists, and dabblers write Perl 5 quite successfully. The language doesn’t have the marketing budget of large consulting companies, hardware manufacturers, or tool vendors pushing it, yet people still use it to get their jobs done. Why argue with that success?

What are the different types of repetition in Perl?

There are a few exceptions though: x can be either string repetition or list repetition, depending on the type of the left operand, and &, |, ^ and ~ can be either string or numeric bit operations. Operator precedence and associativity work in Perl more or less like they do in mathematics.


2 Answers

Jonathan has the answer in response to RT #131695.

The >>[] is a postfix operator to index a list, so it tries to use it as such. This is intended behavior. Fair enough, although I think the parser is being a bit too clever for regular code monkeys here.

like image 101
brian d foy Avatar answered Oct 13 '22 14:10

brian d foy


I think this warrants a rakudobug email. I think the parser gets confused trying to interpret it as a hyper (aka >>.method). The following workaround seems to corroborate this:

my $string = '5+8i';
my $number = <<$string >>[0];  # note space before >>
say $number;

To satisfy your OCD, you could probably also put a space before $string.

And yes, whitespace is not meaningless in Perl 6.

like image 30
Elizabeth Mattijsen Avatar answered Oct 13 '22 14:10

Elizabeth Mattijsen