Interview questions on Perl

What is the difference between my and local?
my creates a new variable; local temporarily amends the value of a variable. Example:

$a = 3.14159;
  local $a = 3;
  print "In block, \$a = $a\n";
  print "In block, \$::a = $::a\n";
print "Outside block, \$a = $a\n";
print "Outside block, \$::a = $::a\n";

# This outputs
In block, $a = 3
In block, $::a = 3
Outside block, $a = 3.14159
Outside block, $::a = 3.14159

local temporarily changes the value of the variable, but only within the scope it exists in.”
Use my whenever you can.
Use local if you need to change the value of a special variable when reading a file.

What is the difference between use and require?
“The use statement works at compile time, require at run time. So if you have a module that does a lot in a begin block and you don’t really need it in all cases, then it’s clever to “require” it there where you want to use it but don’t “use” it. So you don’t have to wait for all that initializations of that module in case you don’t need it.”. Credit goes here.

What is CPAN? Where to go for help when using Perl?
“CPAN is the Comprehensive Perl Archive Network, it’s a good source for anything and everything Perl. For Perl help, there’s a bunch of places including and as well as”. Credit goes here.

What does use strict do?
It requires all variables (subs) to be declared (defined) before they can be used.

How to open and read data files with Perl?

open (CHECKBOOK, "checkbook.txt") || die "couldn't open the file!";
while ($record = < CHECKBOOK >) {
print $record;

Questions and answers below are from here.
How do I sort a hash by the hash key?
Suppose we have a class of five students.
Their names are kim, al, rocky, chrisy, and jane.
Here’s a test program that prints the contents
of the grades hash, sorted by student name:

#!/usr/bin/perl -w

%grades = (
kim => 96,
al => 63,
rocky => 87,
chrisy => 96,
jane => 79,

foreach $key (sort (keys(%grades))) {
print "\t\t$key \t\t$grades{$key}\n";
al 63
chrisy 96
jane 79
kim 96
rocky 87

What algorithm does Perl use for sorting?
Newer versions use “mergesort”. Older versions used “quicksort” which is unstable, meaning it can go quadratic (instead of N \log N) for some inputs.

Write a regular expression that will match a floating point number?
Match optional sign, followed by eiter: 1) one or more digits followed by dot and by one or more digits; or 2)followed by one or more digits. Credit goes here.
In Perl this will read as (I didn’t check this):

$myfloat =~ m/[-+]?([0-9]*\.[0-9]+|[0-9]+)/
This entry was posted in Interview, Perl, Programming, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s