Top 20 Interview Questions in Perl


If you go for an interview for the role of Webmaster, DevOps or SRE, Production Support, Automation etc. You might be asked questions on Perl. Lof of code still exists in Perl (because it’s easy to write). These days python is a lot more common but you will still see a lot of Perl code almost everywhere.

1. What are hashes/associative arrays in Perl

A Perl hash/map/associative arrays/dictionary is a data structure to store key-value pairs.

Examples:

See a hash %x, 3 keys (‘a’, ‘b’, ‘c’) are assigned values.

#!/usr/bin/perl
use strict;
my %x=('a'=>1, 'b'=>2, 'c'=>3);

print %x;
print $x{'a'};


2. Difference between system command and backticks?

System command will not return any data. It will only return status of the command.

backticks will return data and command status can be found in ${^CHILD_ERROR_NATIVE} (from perl v. 5.10). System command will return stats of the command which can be found in $?.


3. How and when to use eval?

eval is used in two forms –

eval EXPRESSION;

eval {BLOCK of CODE}

#!/usr/bin/perl

eval {0/0};
print $@;
print "ABC";

any error is captured in $@;

 


4. Write a regex to check if string is valid phone number (example: 1-111-111-1111)

#!/usr/bin/perl
use strict;
my $s="1-201-264-9860";

if ($s=~/^\d-\d\d\d-\d\d\d-\d\d\d\d$/)
{
print "Match";
}
else
{
print "No Match";
}

 


5. What is map function?

map BLOCK, LIST

Evaluates the BLOCK for each element of LIST

Example: Multiple each element of array by 5.

#!/usr/bin/perl
my @x=(1,2,3);
print map {5*$_."\n"} @x;


6. How can you connect to MySql DB?

Using modules like DBI, you will also need MySQL Drivers too (which are used by DBI module).


7. Difference between next and continue?

Next   it is inside your loop and it will stop the current iteration and go on to the next iteration.

Continue Executed after each loop iteration and before the conditional statement is evaluated. A good place to increment counters. while { } continue { }, after each iteration it will be executed.


8. How do you parse command line arguments?

#!/usr/bin/perl
use strict;
use Getopt::Std;

my %arguments=();
getopts("a:b:c:", \%arguments);

You can also get arguments via simple method:

#!/usr/bin/perl
use strict;
my $x=$ARGV[0];
my $y=$ARGV[1];

 


9. What is %INC?

%INC has list of loaded perl modules and their paths. Keys are module names and values are paths.

 


10. How to remove newline char?

chomp($str);

 


11. How to import a perl module which is not in the current directory?

You can use use

#!/usr/bin/perl
use strict;
use lib '/path/to/module/';
use 'module'
...

 


12. What are BEGIN and END Blocks?

BEGIN: Run some code as soon as it has all been read in by the parser and before the rest compiles. Might be useful to add ENV variables like PATH or modules etc.

The END blocks are the last thing to be evaluated. They are even evaluated after exit() or die() functions are called. Therefore, they can be used to close files or write messages to log files. Multiple END blocks are evaluated in reverse order. If Signals are passed to script they can bypass END blocks.

 


13. What is SWIG?

SWIG is interface compiler which can be used to access c/c++ code/functions in perl (actually python, Tcl too). You will create interface file and then you can access C/C++ code via a module created by swig.


14. How can you sort an array (numbers) and an array (chars)?

@sorted = sort { $a <=> $b } @not_sorted   # numerical sort or

@sorted = sort { $a cmp $b } @not_sorted   # ASCII-betical sort or better

@sorted = sort { lc($a) cmp lc($b) } @not_sorted   # alphabetical sort

 


15. What are isa() and can() methods?

$object->isa( TYPE )), isa returns TRUE if $object is blessed into package TYPE or inherits from package TYPE

can($method) can checks if the object or class has a method called “METHOD” like ($exist=$obj->can(METHOD))

 


16. How to catch signals in Perl?

example:

$SIG{TERM} = sub { die “Caught a sigterm $!” };


17. How do you use data dumper?

#!/usr/bin/perl
use strict;
use Data::Dumper;

my $x={
1=>[a,b,c,],
2=>{1=>2,4=>5}
};
print Dumper($x);


18. What is difference between die and exit?

die will set the error code to $!  if the exception is uncaught. ex : open (RD, “/xyz”) || die “cannot open file – $!”;

exit will set the error code based on its argument. ex exit 5;

 


19. What is difference between @EXPORT and @EXPORT_OK

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.

 


20. What is $?, $0 and $! ?

$? – is exit status of last pipe, backtick or system command

$0 – current perl script filename

$! – current value of errno

Categories

+ There are no comments

Add yours