How can I determine if the set of pars in the Perl code will act as a grouping of parsers or a list?

In perl, brackets are used to override priority (as in most programming languages), as well as to create lists. How can I determine if a particular pair of parsers will be considered as a grouping structure or a list of one element?

For example, I am sure that this is a scalar, and not a single element: (1 + 1)
But what about more complex expressions? Is there an easy way to tell?

+7
list perl parentheses grouping
source share
2 answers

Three key principles are helpful here:

Context is king. The evaluation of your example (1 + 1) depends on the context.

 $x = (1 + 1); # Scalar context. $x will equal 2. Parentheses do nothing here. @y = (1 + 1); # List context. @y will contain one element: (2). # Parens do nothing (see below), aside from following # syntax conventions. 

In a scalar context, there is no such thing as a list . To see this, try assigning what appears to be a list to a scalar variable. A way to think about this is to focus on the behavior of the comma operator: in a scalar context, it evaluates its left argument, returns that value, then evaluates its right argument and returns that value. In the context of a list, a comma operator inserts both arguments into a list.

 @arr = (12, 34, 56); # Right side returns a list. $x = (12, 34, 56); # Right side returns 56. Also, we get warnings # about 12 and 34 being used in void context. $x = (@arr, 7); # Right side returns 7. And we get a warning # about using an array in a void context. 

Parentheses do not create lists . The comma operator creates a list (provided that we are in the context of the list). When typing lists in Perl code, parentheses are required for priority, not list creation. A few examples:

  • Parentheses do not affect: we evaluate the array in a scalar context, so the right side returns the size of the array.

     $x = (@arr); 
  • Parentheses are not needed to create a single-item list.

     @arr = 33; # Works fine, with @arr equal to (33). 
  • But parentheses are needed with several elements - for priority reasons.

     @arr = 12, 34, 56; # @arr equals (12). And we get warnings about using # 34 and 56 in void context. 
+16
source share
  • Context.
  • The brackets do not have the role you think when creating the list.

Examples:

 $x = 1 + 1; # $x is 2. $x = (1 + 1); # $x is 2. @x = 1 + 1; # @x is (2). @x = (1 + 1); # @x is (2). $x = (1 + 1, 1 + 2); # $x is 3. @x = (1 + 1, 1 + 2); # @x is (2, 3). 

Roughly speaking, in the context of a list, a comma operator separates the elements of a list; in a scalar context, the comma operator is C "sequential comma", which evaluates its left and right sides and returns the value of the right side. In the scalar context, expressions in parentheses to override the order of operations and in the context of a list, parentheses ... are really the same. The reason they relate to the purpose of arrays is this:

 # Comma has precedence below assignment. # @a is assigned (1), 2 and 3 are discarded. @a = 1, 2, 3; # @a is (1, 2, 3). @a = (1, 2, 3); 

As for your question โ€œis it a scalar or a singleton listโ€, it just doesnโ€™t make sense to ask the expression in isolation because of the context. In the context of a list, everything is a list; in a scalar context, there is nothing.

Recommended reading: perlop , perldata , Perl Programming.

+8
source share

All Articles