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:

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

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

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

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:

And we get this as result:

 

Turtle Operator

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

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

 

Inchworm Operator

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

And we see its the same result:

 

Goatse Operator

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

And we got 3 matches :

 

Venus Operator

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

and we get our numbers:

 

Bool Operator

the Bool Operator casts a number to a bool value:

and it only gets printed if its 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:

prints this:

while example:

prints this:

for loop print matches and position:

prints this:

 

Placeholder

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

 

More Examples

print all odd numbers:

Result looks like:

 

make a flip flop from 1 beginning:

Result is:

 

select elements and make quick print:

Prints this:

One thought on “Perl Tricks”

Leave a Reply

Your email address will not be published.

16 + 1 =