I am currently documenting all of Perl 5\'s operators (see the perlopref GitHub project) and I have decided to include Perl 5\'s pseudo-operators as well. To me, a pseudo-o
The << >>
operator, for multi-line comments:
<<q==q>>;
This is a
multiline
comment
q
Don't forget the Flaming X-Wing =<>=~
.
The Fun With Perl mailing list will prove useful for your research.
From this question, I discovered the %{{}}
operator to cast a list as a hash. Useful in
contexts where a hash argument (and not a hash assignment) are required.
@list = (a,1,b,2);
print values @list; # arg 1 to values must be hash (not array dereference)
print values %{@list} # prints nothing
print values (%temp=@list) # arg 1 to values must be hash (not list assignment)
print values %{{@list}} # success: prints 12
If @list
does not contain any duplicate keys (odd-elements), this operator also provides a way to access the odd or even elements of a list:
@even_elements = keys %{{@list}} # @list[0,2,4,...]
@odd_elements = values %{{@list}} # @list[1,3,5,...]
In Perl these are generally referred to as "secret operators".
A partial list of "secret operators" can be had here. The best and most complete list is probably in possession of Philippe Bruhad aka BooK and his Secret Perl Operators talk but I don't know where its available. You might ask him. You can probably glean some more from Obfuscation, Golf and Secret Operators.
How about the "Boolean one-or-zero" operator: 1&!!
For example:
my %result_of = (
" 1&!! '0 but true' " => 1&!! '0 but true',
" 1&!! '0' " => 1&!! '0',
" 1&!! 'text' " => 1&!! 'text',
" 1&!! 0 " => 1&!! 0,
" 1&!! 1 " => 1&!! 1,
" 1&!! undef " => 1&!! undef,
);
for my $expression ( sort keys %result_of){
print "$expression = " . $result_of{$expression} . "\n";
}
gives the following output:
1&!! '0 but true' = 1
1&!! '0' = 0
1&!! 'text' = 1
1&!! 0 = 0
1&!! 1 = 1
1&!! undef = 0
You have two "countof" (pseudo-)operators, and I don't really see the difference between them.
From the examples of "the countof operator":
my $count = ()= f(); #$count is now 5
my $string = "cat cat dog cat";
my $cats = ()= $string =~ /cat/g; #$cats is now 3
From the examples of "the goatse/countof operator":
my $count =()= f(); #$count is now 5
my $string = "cat cat dog cat";
my $cats =()= $string =~ /cat/g; #$cats is now 3
Both sets of examples are identical, modulo whitespace. What is your reasoning for considering them to be two distinct pseudo-operators?