Perl Tricks

here is a list of Operator in Perl that are not really known, but they are very useful and could make your code shorter but harder to read.

 

Undefined Operator

This examples check if the value is undefined:

my $x=undef;

print $x.$/;

$x //= "undef";

print $x.$/;

it was undefined so the //= operator set it to undef.

undef

A little bit more complex is this, her I check if the value is false or undefined:

my ($x,$y)=("",undef);

$x //= "undef";# is undef
$x ||= "false";# is false

$y //= "undef";# is undef
$y ||= "false";# is false

print $x.$/;
print $y.$/;

and we see $x was false and $y was undefined.

false
undef

Nice to check if the vales are defined and true.

 

String as array

here i print every char in to the next line, its a good solution if you need to iterate to a string like an array:

while("Hello World!n" =~ /(.)/g) {
print "$1\n";
}

And we get this as result:

H
e
l
l
o
 
W
o
r
l
d
!
n

 

Turtle Operator

The Turtle Operator could run some code in a string, sounds confusing but is sometimes a very nice feature:

print "2 * 4 = @{[2*4]} \n";
print "Time : @{[scalar localtime]} \n";

and we see he calculates and run the local time function:

2 * 4 = 8 
Time : Thu Mar 25 18:02:26 2015

 

Inchworm Operator

Its not very special, its the same a “scalar” but shorter:

print scalar localtime,$/;
print ~~ localtime,$/;

And we see its the same result:

Time : Thu Mar 25 18:08:49 2015 
Time : Thu Mar 25 18:08:49 2015

 

Goatse Operator

Makes an list assignment in scalar context and returns the elements from the right side:

my $t = "1 2 3 4 1 5 1";
my $c =()= $t =~ /1/g;
print $c.$/;

And we got 3 matches :

3

 

Venus Operator

The Venus Operator makes a cast to a number, not to int !

print 0+"6gh".$/;
print 0+'3.99'.$/;
print 0+'1e4'.$/;

and we get our numbers:

6
3.99
10000

 

Bool Operator

the Bool Operator casts a number to a bool value:

print "false"x!!0;
print "true"x!!4;

and it only gets printed if its true:

true

 

Tadpole Operator

The Tadpole Operator prints the current loop position.I must be in scalar context and like to use it with the Goatse Operator and Inchworm Operator from above.

for example:

for('A'..'D'){
print "$_ -> @{[~~($$..!$$)]}$/";
}

prints this:

A -> 1
B -> 2
C -> 3
D -> 4

while example:

while("Hello World!" =~ /(.)/g) {
print "@{[~~($$...!$$)]} = $1\n";
}

prints this:

1 = H
2 = e
3 = l
4 = l
5 = o
6 =  
7 = W
8 = o
9 = r
10 = l
11 = d
12 = !

for loop print matches and position:

for(0..10){
$_%2 and next;
print "$_ -> @{[~~($$..!$$)]}$/";
}

prints this:

0 -> 1
2 -> 2
4 -> 3
6 -> 4
8 -> 5
10 -> 6

 

Placeholder

you can just type “…” in your code a placeholder.

sub calculate {
...
}

 

More Examples

print all odd numbers:

for(0..10){
print "$_ -> @{[~~($$..!$$)]}$/"x!!($_%2);
}

Result looks like:

1 -> 2
3 -> 4
5 -> 6
7 -> 8
9 -> 10

 

make a flip flop from 1 beginning:

for(0..10){
print "$_ -> @{[($_...$_)-1]}$/";
}

Result is:

0 -> -1
1 -> 0
2 -> 1
3 -> 0
4 -> 1
5 -> 0
6 -> 1
7 -> 0
8 -> 1
9 -> 0
10 -> 1

 

select elements and make quick print:

my @arr = ("1"x!!1,"2"x!!0,"3"x!!2,"4"x!!0,);
print for(@arr);

Prints this:

13

One thought on “Perl Tricks”

Leave a Reply

Your email address will not be published.

2 × three =