Questions tagged [goatse]

The Goatse Pseudo-operator takes a list on its right hand side and returns the number of elements in the list in scalar context (and an empty list in list context) . my $str = "1,2 cha cha cha, 3,4 cha cha cha"; my $count =()= $str =~ /\d/g; print $count; Output will be 4. (1,2,3 & 4).

The Goatse Pseudo-Operator takes a list on its right hand side and returns the number of elements in the list in scalar context (and an empty list in list context).

my $str = "1,2 cha cha cha, 3,4 cha cha cha";
my $count =()= $str =~ /\d/g;
print $count;

Output will be 4. (1,2,3 & 4)

The actual operators and terms involved are (from left to right) scalar assignment: =, the empty list term: (), and list assignment: =. It takes advantage of the fact that list assignment returns the number of items on the right hand side when called in scalar context:

my $count = my ($x, $y, $z) = ("a" .. "z");
print "$count $x $y $z\n";

Output will be: 26 a b c

By specifying an empty list, you spare perl from having to copy the values in the list, but it still returns the number of elements on the right hand side. The following two lines are identical in results:

my $count = () = ("a" .. "z"); #formatted as separate operators
my $count =()= ("a" .. "z");   #formatted as the goatse operator

The name comes from a picture distributed by a website designed to shock people. Searching for the term may provide more insight into the name choice, but it is not advised.

2 questions
27
votes
3 answers

Is the Perl Goatse 'Secret Operator' efficient?

The "goatse operator" or the =()= idiom in Perl causes an expression to be evaluated in list context. An example is: my $str = "5 and 4 and a 3 and 2 1 BLAST OFF!!!"; my $count =()= $str =~ /\d/g; # 5 matches... print "There are $count numbers in…
dawg
  • 98,345
  • 23
  • 131
  • 206
12
votes
3 answers

Why does the goatse operator work?

The difference between arrays and lists and between list and scalar context have been discussed in the Perl community quite a bit this last year (and every year, really). I have read over articles from chromatic and friedo, as well as this…
Nate Glenn
  • 6,455
  • 8
  • 52
  • 95