A simple perl recursion example

Have a test about what will happen if a script is calling to run it self when it is running:

use strict;
use warnings;

print "******\n";
perl $0;

The result will be kind of infinite recursion, keep printing out the * .

Most common build-in functions or operators beginners should know about Perl

if (defined $x)

check a value or variable is undef or not.

undef $x;

reset a variable to undef.

qq, double-q operator; q, single-q operator

print qq(The "name" is "$name"\n);
print qq(The (name) is "$name"\n);
print qq{The )name( is "$name"\n};
print q[The )name} is "$name"\n];

The result will be:

The "name" is "foo"
The (name) is "foo"
The )name( is "foo"
The )name} is "$name"\n

x is repetition operator.

say "2" x 4;

will print the result:



use Scalar::Util qw(looks_like_number);
my $z = 3;
say $z;
my $y = "3.14";
if (looks_like_number($z) and looks_like_number($y)) {
    say $z + $y;

String comparison operators: eq, ne, lt, gt, le, ge

String functions: length, lc, uc, index, substr

my $str = "The black cat climbed the green tree";
say index $str, 'cat'; # 10
my $str = "The black cat climbed the green tree";
say substr $str, 4, 5; # black

int(): Integer part of a fractional number

rand(): Random numbers, A call to the rand($n) function of Perl will return a random fractional number between 0 and $n. It can be 0 but not $n.

last: skip the rest of the block and won’t check the condition again.

while (1) {
   print "Which programming language are you learning now? ";
    my $name = <STDIN>;
    chomp $name;
    if ($name eq 'Perl') {
    say 'Wrong! Try again!';
say 'done';

exit: exit the script running

Redirect standard output or standard error:

perl test.pl > output_log
perl test.pl 2 > error_log 
perl test.pl 2 > /dev/null #black hole

print scalar keys %hash;

size of the hash.

&func1 #function call

$_ #default input and pattern-searching space

$. current line number for the last filehandler accessed

$0 name of the programming being executed

$$ process number of the Perl running the script

$! yield the current value of error message

$@ the perl syntax error message from the last eval() operator

What does "my" really mean?

"The meaning of my $x is that you tell perl, and specifically to strict, that you would like to use a private variable called $x in the current scope. Without this, perl will look for a declaration in the upper scopes and if it cannot find a declaration anywhere it will give a compile-time error Global symbol requires explicit package name Every entry in a block, every call to a function, every iteration in a loop is a new world. "

Empty an existing variable:

$x = undef;
@a = ();
%h = ();

Source from: perlmaven

Perl confusing examples

  1. Confusing output order:

    use warnings; use strict; use 5.010;

    print 'OK'; my $x; say $x; $x = 42; say $x;

This code prints statements before the line generating the warning, the result might be confusing:

Use of uninitialized value $x in say at perl_warning_1.pl line 7.

The reason is that by default Perl buffers STDOUT, while not buffer STDERR.

Turn off the buffering of STDOUT by: $| = 1; at the beginning of the script.

  1. Second most common warning: Use of uninitialized value:

Usually means variables have no value: either it never got a value, or at some point undef was assigned to it.

  1. Unknown warning category 'xxxx':

    use strict; use warnings

    print "Hello World";

Missing semicolon after using warnings statement. Perl is executing the print function and returns 1 indicating that is was successfull. changed to:

use warnings 1
  1. Boolean values in Perl:

    The number 0, the strings '0' and '', the empty list "()", and "undef" are all false in a boolean context. All other values are true. Negation of a true value by "!" or "not" returns a special false value. When evaluated as a string it is treated as '', but as a number, it is treated as 0.

References: Perl Maven Tutorial

Debuging method for perl Complex data structures

For complex data structures (references, arrays and hashes) you can use the Data::Dumper

use Data::Dumper qw(Dumper);

print Dumper \@an_array;
print Dumper \%a_hash;
print Dumper $a_reference;

These will print something like this, which helps understand the content of the variables, but shows only a generic variable name such as $VAR1 and $VAR2.

$VAR1 = [
$VAR1 = {
       'a' => 1,
       'b' => 2
$VAR1 = {
       'c' => 3,
       'd' => 4

I'd recommend adding some more code and printing the name of the variable like this:

print '@an_array: ' . Dumper \@an_array;

to gain:

@an_array: $VAR1 = [

or with Data::Dumper like this:

print Data::Dumper->Dump([\@an_array, \%a_hash, $a_reference],
[qw(an_array a_hash a_reference)]);


$an_array = [
$a_hash = {
          'a' => 1,
          'b' => 2
$a_reference = {
               'c' => 3,
               'd' => 4