≡ Menu

5 Useful Perl 5.10 Features – Say, State, ~~, Defined OR

Even though Perl 5.10 is an old release, there are few things that was introduced in Perl 5.10, which you might find it useful.

In the future article of this series, we’ll be covering new features of Perl 5.14

These are the 5 new Perl 5.10 features that you might find helpful.

1. Say it instead of Print

The major difference between say and print is, say prints a newline at the end by default. So, when you use “say”, you don’t need to give the “\n” at the end.
Both of the following does the exact same thing:

print "Hello World!\n";
say "Hello World!";

So most of the programmers would find it handy as they print newline at the end of message mostly.

2. Named Captures in Regex

Parenthesis can be used to do named capturing in this format: (?<name>REGEX)

The text captured by the regular expression can be accessed by its name as key in the hash named +

$+{name}

The same way to access the named capture in the substitute you can use the following method – which is similar to \1 or \2 in capturing parenthesis.

\g{name}

Example: The following code snippet demonstrates named capture use after match and during match.

use v5.10;

$_ = 'bob23';

if ( /(?<username>\D+)(?<age>\d+)/ ) {
	say "User $+{username}'s age is $+{age}";
}

$userdetails = 'Username:sathiya, password:sathiya';

if ( $userdetails =~ /Username:(?<username>.*), password:(\g{username})/ )  {
	say "Username and password are identical for $+{username}";
}
  • In the first pattern match, username and age are captured and used from + hash after match.
  • In the second pattern match, username is captured and used during match with \g.

Note: Even if you have different names for capture you wont have it after next match. Remember to store the named capture to other variable because the next match will reset this hash %+.

3. State – Persistent Variable across Function Calls

When you want a variable to hold data across function calls in a function, a simple way earlier is to use global variables but that would allow manipulation of that variable from anywhere.

In perl 5.10 ‘state’ would be a perfect solution for that.

Example: Counter variable to maintain how many times a function has been accessed.

use v5.10;

sub func {
    state $accessed = 0;
    say 'function accessed ', ++$accessed , ' times';
}

&func();
&func();
&func();

This prints the following output when executed:

function accessed 1 times
function accessed 2 times
function accessed 3 times

The variable $accessed is initialized only once.

4. Defined OR Operator ( // )

Difference between OR operator and Defined OR operator

$result = $a || $b;
$result = $a // $b;
  • OR Operator: $b will be stored in $result if $a has value 0 or empty.
  • Defined OR Operator: $b will be stored in $result only if $a is not defined. But if $a has 0 or empty, then $a value will be stored into $result.

5. Smart Match Operator ( ~~ )

It can act as == or eq according to the data on both sides.

Example:

if ( 4 ~~ "4.0" )  {
  print 'matched'
} else {
  print 'not matched'
}

This will print ‘matched’. But if you use -eq operator instead it will try to do a string comparison and print ‘not matched’.

Smart match is more smarter, as it can compare a string against a list, hash with hash, etc.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • gopal January 30, 2012, 5:08 am

    If you want to use the new features in a one-liner, you can do so with the “-E” (instead of “-e”) Argument:

    perl -E ‘say 42’

    By the way – you are a bit behind with your news. The current stable version of Perl is 5.14, the next stable version 5.16 is due in June, as Perl’s development is on an annual release cycle now. You can read about the new features in 5.14 here, the most useful are the the “\r” regex modifier for non-destructive substitution and the fact, that “use strict;” is automatically on.

    If your distribution doesn’t have an up-to-date version of Perl you can install your own trivially and with out messing around with the system Perl with “perlbrew” from here.

  • Rick Stanley January 30, 2012, 9:24 am

    Sorry, but Perl 5.10, is not exactly new. From the Wikipedia page, ,
    “On December 18, 2007, the 20th anniversary of Perl 1.0, Perl 5.10.0 was released.”

    Later releases: 5.11.0, 5.12.0, 5.12.3, 5.12.4, 5.14.0, 5.14.2. The latest development release of Perl 5 is 5.15.4, released by Jesse Vincent on October 20, 2011. Many new features, along with many bug fixes, have been added since 5.10.0.

    A new version, Perl 6, is in development. If your Distro is still running 5.10, I would reccomend upgranding or replacing to a current Distro. Debian is highly reccomended. See: here and here.

  • Gabor Szabo February 2, 2012, 1:49 am

    Why use &func(); ? You don’t need the leading & since perl 5 came out more than 15 years ago.

    Please, refrain from using $a and $b other than in “sort”. They are special variables that can easily conuse some people as they don’t need to be declared using “my”.

    BTW the most common usaged of the defined-or operator is to assign default values:

    $filename //= $SOME_DEFAULT_FILENAME;