EzDevInfo.com

perl interview questions

Top perl frequently asked interview questions

What are the differences between Perl, Python, AWK and sed? [closed]

just want to know what are the main differences among them? and the power of each language (where it's better to use it).

Edit: it's not "vs." like topic, just information.


Source: (StackOverflow)

How do I compare two strings in Perl?

How do I compare two strings in Perl?

I am learning Perl, I had this basic question looked it up here on StackOverflow and found no good answer so I thought I would ask.


Source: (StackOverflow)

Advertisements

How can I fix a locale warning from Perl?

When I run perl, I get the warning:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

How do I fix it?


Source: (StackOverflow)

What's the difference between Perl's backticks, system, and exec?

Can someone please help me? In Perl, what is the difference between:

exec "command";

and

system("command");

and

print `command`;

Are there other ways to run shell commands too?


Source: (StackOverflow)

How can I pass command-line arguments to a Perl program?

I'm working on a Perl script. How can I pass command line parameters to it?

Example:

script.pl "string1" "string2"

Source: (StackOverflow)

Why is this program valid? I was trying to create a syntax error

I'm running 32 bit ActiveState 5.14.2 on Windows 7. I wanted to mess around with a git pre-commit hook to detect programs being checked in with syntax errors. (Somehow I just managed to do such a bad commit.) So as a test program I randomly jotted this:

use strict;
use warnings;

Syntax error!

exit 0;

However, it compiles and executes with no warnings, errorlevel is zero on exit. How is this valid syntax?


Source: (StackOverflow)

Find size of an array in Perl

I seem to have come across several different ways to find the size of an array. What is the difference between these three methods?

my @arr = (2);
print scalar @arr; # First way to print array size

print $#arr; # Second way to print array size

my $arrSize = @arr;
print $arrSize; # Third way to print array size

Source: (StackOverflow)

What is the difference between my and our in Perl?

I know what my is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our do? How does our differ from my?


Source: (StackOverflow)

How do you round a floating point number in Perl?

How can I round a decimal number (floating point) to the nearest integer?

e.g.

1.2 = 1
1.7 = 2

Source: (StackOverflow)

How can I check if a Perl array contains a particular value?

I am trying to figure out a way of checking for the existence of a value in an array without iterating through the array.

I am reading a file for a parameter. I have a long list of parameters I do not want to deal with. I placed these unwanted parameters in an array @badparams.

I want to read a new parameter and if it does not exist in @badparams, process it. If it does exist in @badparams, go to the next read.


Source: (StackOverflow)

How can I quickly sum all numbers in a file?

I have a file which contains several thousand numbers, each on it's own line:

34
42
11
6
2
99
...

I'm looking to write a script which will print the sum of all numbers in the file. I've got a solution, but it's not very efficient. (It takes several minutes to run.) I'm looking for a more efficient solution. Any suggestions?


Source: (StackOverflow)

How is Perl's @INC constructed? (aka What are all the ways of affecting where Perl modules are searched for?)

What are all the ways of affecting where Perl modules are searched for? or, How is Perl's @INC constructed?

As we know, Perl uses @INC array containing directory names to determine where to search for Perl module files.

There does not seem to be a comprehensive "@INC" FAQ-type post on StackOverflow, so this question is intended as one.


Source: (StackOverflow)

How do I use boolean variables in Perl?

I have tried:

$var = false;
$var = FALSE;
$var = False;

None of these work. I get the error message

Bareword "false" not allowed while "strict subs" is in use.

Source: (StackOverflow)

What exactly does Perl's "bless" do?

I understand one uses the "bless" keyword in Perl inside a class's "new" method:

sub new {
    my $self = bless { };
    return $self;
}

But what exactly is "bless" doing to that hash reference ?


Source: (StackOverflow)

How do I get the full path to a Perl script that is executing?

I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0 varies and sometimes contains the fullpath+filename and sometimes just filename. Because the working directory can vary as well I can't think of a way to reliably get the fullpath+filename of the script.

Anyone got a solution?


Source: (StackOverflow)