Perl: Diamon operator: <> , $_, the default variable

The Diamond operator is almost exclusively used in a while-loop. It allows us to iterate over the rows in all the files given on the command line.

Diamond – using $_, the default variable

We can make the above example even more compact by remembering that certain operation in Perl will useĀ $_, the default variable of Perl, if no explicit variable is given.

use strict;
use warnings;
 
while (<>) {
    print if /perl/;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *