How to use Perl’s ternary operator

July 6th, 2009 Alex Posted in perl, programming Comments Off

The Ternary Operator

The ternary is actually a sequence of operators. The operator is used like this:

CONDITION-PART ? TRUE-PART : FALSE-PART

which is shorthand for the following statement:

if (CONDITION-PART) {

TRUE-PART

} else {

FALSE-PART

}

Example: If $firstVar is zero, then assign $secondVar a value of zero. Otherwise, assign $secondVar the value in the first element in the array @array.

$secondVar = ($firstVar == 0) ? 0 : $array[0];

via Perl 5 By Example.

AddThis Social Bookmark Button

Check String for Non-ASCII Characters in Perl

November 1st, 2008 Alex Posted in perl, programming Comments Off


if ( $string =~ /[[:^ascii:]]/ ) {
print "String contains characters that are NOT pure ASCII";
}
else {
print "Everything is good, string/pure is valid ASCII.";
}

E.G.: If it contains bytes > 127, it’s not valid ASCII.

AddThis Social Bookmark Button

Active Perl’s Package Manager: ppm

August 6th, 2008 Alex Posted in perl, programming, slackware Comments Off

It allows you to get Perl pre-compiled packages. You can avoid installing a C compiler which is needed for some packages (KinoSearch for example).

http://aspn.activestate.com/ASPN/Downloads/ActivePerl/PPM/

Alternate packages repositories:
http://cpan.uwinnipeg.ca/PPMPackages/10xx/

AddThis Social Bookmark Button

Perl’s KinoSearch vs Plucene

July 17th, 2008 Alex Posted in linux, perl, programming, windows Comments Off

KinoSearch is the winner

The following perl scripts index *.txt in the current folder and search for “TEST”. The first one is using Perl’s KinoSearch module and the other one is using Plucene. KinoSearch is alot faster then Plucene and also gives better results.

(right click to download)

KinoSearch.pl

Plucene.pl

AddThis Social Bookmark Button