Random Perl Snippet

Something that I get to do occasionally at work is review old code to fix some random bug. I actually enjoy doing this because I rarely get to program anymore (since I have never been a developer) and it’s nice to brush up on it again. The other day, I ran into some code I hadn’t seen before, that is valid Perl, but is much more C like:

return ($n < 0) ? 0 : $n;

The biggest problem with trying to figure out what this does if you’ve never seen it before is that you have no idea what to search for. Do you look up “?:”, no, because that actually means something else. Google Code Search has been my friend in the past on things like this, because it can handle all the special characters. I highly recommend trying it out. This time I figured out it was an old C construct, so I just went over and asked a developer what it meant.

So, for those who didn’t know already, this is what it means:

($n < 0) – this is the statement that is being checked (like in an if statement).

The part between the ? and the : is what is returned if the statement is true. The part after the : is what is returned if the statement is false.

So in this case, the author was making sure that the number returned was positive. If $n was less than zero (or negative), it would return zero. If it was zero or some positive number, it just returned that number.

Now, why would you do this? Well, it would all depend on how the variable was derived in the subroutine above it. In my case, the author was using a creative method to find the total number of a specific character in a string (in this case the slash):

my $str = shift(@_);
my $n = split('/', $str) - 1;

Now, although this works, Perl will complain about it because you are clobbering the array @_ and consequently your subroutine arguments. So I ended up changing the code to use the translate command “tr”, which returns the number of matches:

my $n = ($str =~ tr////);

Of course, now the return code is irrelevant, but not causing any harm…





Please VOTE for this page at: ADD TO DEL.ICIO.US | ADD TO DIGG | ADD TO FURL | ADD TO NEWSVINE | ADD TO NETSCAPE | ADD TO REDDIT | ADD TO STUMBLEUPON | ADD TO TECHNORATI FAVORITES | ADD TO SQUIDOO | ADD TO WINDOWS LIVE | ADD TO YAHOO MYWEB | ADD TO ASK | ADD TO GOOGLE


2 Comments


  1. That is called a ‘Tertiary’ operator, and is basically an operator that takes three arguments. Think of it as a condensed if..then..else statement. (condition) ? (true) : (false)

    I use them all the time in PHP to shorten simple conditionals from five lines to one.

    if (condition) {
    $value = something
    } else {
    $value = something else
    }

    Versus

    $value = (condition) ? 'something' : 'something else';

    Posted April 13, 2009, 1:22 pm

  2. I did eventually find it in my “Perl 5 Developer’s Guide” called “The Conditional Operator”, which is poorly named since there are multiple conditional operators in Perl. I did find instances on the web where it is called the tertiary operator in Perl as well. Anyway, I also learned that it can also handle an “elsif” scenario like this:

    $value = ($condition1) ? $value1 : ($condition2) ? $value2 : $value3;

    Kevin

    Posted April 13, 2009, 1:42 pm

Leave a reply