Javatpoint Logo
Javatpoint Logo
Perl Interview Questions

Perl Interview Questions

1) What is Perl?

Perl stands for Practical Extraction and Reporting Language. It is a high-level programming language written by Larry Wall, specially designed for processing text. It supports object-oriented programming, and its syntax is quite similar to C language. It is much more flexible to use. Due to its strong text processing abilities, it has become one of the most popular languages for writing CGI scripts.

For more information: click here.


2) Is Perl Compiler or Interpreter?

Perl is said to be both compiler and interpreter. It reads the source code, converts the program into bytecode before execution and then runs it. Hence, Perl is sometimes called an interpreter/compiler.


3) Which operator in Perl is used for the concatenation of two strings?

The operator used for the concatenation of strings is (.) operator.


4) What is CPAN in Perl?

CPAN stands for Comprehensive Perl Archive Network. It is a repository which contains thousands of Perl modules.


5) What are the features of Perl language?

The feature of Perl:

  • It has a very simple Object-oriented programming syntax.
  • It is easily extendible as it supports 25,000 open source modules.
  • It supports Unicode.
  • Includes powerful tools to process text to make it compatible with markup languages like HTML, XML.
  • It supports third-party database including Oracle, MySQL, and many others.
  • It is embeddable in other systems such as web servers and database servers.
  • It is open source software licensed under GNU.
  • Many frameworks are written in Perl.
  • It can handle encrypted web data including e-commerce transactions.
  • It is a cross-platform language.
  • It offers a regular expression engine which can transform any text.

For more information: click here.


6) What are the advantages and disadvantages of Perl language?

Perl advantages:

  • Its syntax is simple, which makes it easy to understand.
  • It supports OOP concepts.
  • Perl programs run easily on the system.
  • It is more flexible to use.
  • It supports all platforms and is much more portable.
  • It has a rich set of Perl modules and free software.
  • It is much more efficient to work on text and string manipulation as it is a scripting language.
  • Perl combines features of many other languages which make it easy to understand.
  • Perl disadvantages

  • A Perl program containing CPAN modules will not run on another system which doesn't have CPAN modules installed.
  • It is an interpretative language, so it is slower in comparison with other languages.
  • It has untidy and unreadable codes.
  • It starts creating problems when the code is larger than 200 lines.
  • It is not portable.

  • 7) Define print() function in Perl?

    The Perl print() function prints anything it gets as its argument.


    8) Define say() function in Perl?

    The Perl say() function is not supported by older Perl versions. It is like Perl print() function with only one difference that it automatically adds a new line at the end.


    9) Define dynamic scoping.

    The assignment of temporary values to the global variables is called as dynamic scoping.


    10) Explain lexical variables.

    The variables which are created by the use of 'my' operator and private in nature are called as lexical variables.


    11) When does circular reference occur?

    When the two references also contain references to each other, then the case of circular reference occurs.


    12) What happens in dereferencing?

    Value to the location from the reference point is returned.


    13) What does read () command do?

    The read() command is used to read the block of information from the buffered file handle.


    14) What is the use of 'ne' operator?

    The operator compares the value of the left string to the right one and returns true if they are not equal.


    15) What does the q{ } operator do?

    The operator encloses a string in the single quotes.


    16) What does the qq{ } operator do?

    The operator encloses a string in the double quotes.


    17) What does the qx{ } operator do?

    The operator encloses a string in the inverted quotes.


    18) How many data types are there in Perl?

    Perl has three data types:

    • Scalars
    • Arrays
    • Hashes

    For more information: click here.


    19) What are Perl variables?

    A variable is a place to store values reserving some memory space. Perl treats the same variables differently based on context.

    There are three types of Perl variables:

    • Scalars
    • Arrays
    • Hashes

    For more information: click here.


    20) What are scalars?

    A scalar contains a single unit of data. They are preceded with a ($) sign. A scalar contains a number, character, reference or a string. A reference is the address of the variable.

    For more information: click here.


    21) What are arrays in Perl?

    An array contains an ordered list of scalar values. It is preceded with (@) sign. To access a single element in a Perl array ($) sign is used.

    For more information: click here.


    22) How to find the length of an array in Perl?

    Size of an array is determined with scalar context on the array. Array length will always be one greater than its largest index.

    Perl size = $#arrayName +

    Where $#arrayName is the maximum index of the array.

    For more information: click here.


    23) What are Perl array functions?

    Perl array functions are used to add or remove some elements in an array.

    There are four types Perl array functions:

    • Push
    • Pop
    • Shift
    • Unshift

    For more information: click here.


    24) What is Perl push array function?

    The Perl push array function appends a new element at the end of an array.

    For more information: click here.


    25) What is Perl pop array function?

    The Perl pop array function removes the last element of an array.

    For more information: click here.


    26) What is Perl shift array function?

    The Perl shift array function removes the leftmost element from the array shortening array by 1.

    For more information: click here.


    27) What is Perl unshift array function?

    The Perl shift array function adds a new element at the start of an array.

    For more information: click here.


    28) How to replace Perl array elements?

    The Perl splice array function removes elements and replaces them with the specified list of elements.

    For more information: click here.


    29) How to convert strings into an array in Perl?

    The Perl split array function splits a string into an array of strings. Thus converting strings into an array.

    For more information: click here.


    30) How to convert arrays into a string in Perl?

    The Perl join array function combines more than one array into a single string. Thus converting arrays into a string.

    For more information: click here.


    31) How to merge two arrays in Perl?

    The Perl merged array function merges two arrays into a single array by removing all the commas in between them.

    For more information: click here.


    32) How to sort arrays in Perl?

    The Perl sort array function sorts all the elements of an array according to the ASCII standard.

    For more information: click here.


    33) What are the hashes?

    A Perl hash is a group of unordered key-value pairs. The keys are unique strings and values are scalar values. It is preceded with (%) sign. They can be accessed using their key values.

    For more information: click here.


    34) How to know whether a key exists or not in Perl?

    Using Perl exists function, you can check whether a key exists or not in a hash. It returns true if the key exists.

    For more information: click here.


    35) How to add elements in a hash in Perl?

    To add a new key-value pair in the hash, declare them as a single variable in the hash variable.

    For more information: click here.


    36) What does delete function do in Perl?

    To remove a hash element, use delete function. It removes both key and value element from the hash.

    For more information: click here.


    37) What does undef function in Perl?

    The undef function removes the value from the hash, but its key remains there.

    For more information: click here.


    38) What is the difference between Perl array and Perl hash?

    Perl array: They are an ordered list of elements, positioned by index number. It is denoted with @ sign.

    Perl hash: They are an unordered list of elements, positioned by their key values. It is denoted with a % sign.


    39) What is the difference between Perl list and Perl array?

    Perl list is a method to organize data in the Perl source code. It is a fixed collection of scalars. They are always one dimensional.

    Perl array is a method to store data in the form of variables. They are multi-dimensional.


    40) What is the difference between use and require in Perl?

    Use: It is used only for the Perl modules. The included modules are verified at the time of compilation. It does not need the file extension.

    Require: It is used for both Perl modules and libraries. The included objects are verified at runtime. It does need the file extension.


    41) How many loop control keywords are there in Perl?

    There are three types of loop control statement:

    • Next
    • Last
    • Redo

    42) What does next statement do in Perl?

    Perl next statement is like continue statement in C. It lets you move on to the next element of your array or hash skipping all elements in between.

    For more information: click here.


    43) What does last statement do in Perl?

    Perl next statement is like break statement in C. It exists the loop immediately skipping remaining codes.

    For more information: click here.


    44) What does redo statement do in Perl?

    Perl redo statement restarts the current loop without evaluation of the control statement.

    For more information: click here.


    45) Define operators used in Perl?

    A Perl operator is a series of symbols like +, -, =, <, >, etc. It uses its operands as arguments.

    • Pattern matching operator : (=~, !~)
    • Shifting operator : (>>, <<)
    • Comparison operator : (==, !=, <=, >=, <=>)
    • Logical operator : &&, ||

    For more information: click here


    46) What is the importance of Perl warnings?

    Perl warnings help us to check errors in our code by giving warnings.

    To enable them use -w :

    Alternatively, you can also supply it within the "shebang" line:


    47) Why do we use "use strict" in Perl?

    The "use strict" command in Perl calls strict pragma. This pragma helps to catch some bugs or errors in our script and stops the program execution.

    For more information: click here.


    48) What are Perl strings?

    Strings are an essential part of Perl. They are scalars, so they start with $ sign. Strings can be placed inside single or double quote.

    Two types of string operators are there:

    • Concatenation (.)
    • Repetition (x)

    For more information: click here


    49) What is an interpolation in Perl?

    Interpolation means inserting something with different nature. It can be defined as replacing a variable with its value.


    50) What is the difference between single (') and double (") quote in a string in Perl?

    In the single quote, the value is printed as it is given inside the string without interpolation.

    In the double quote, the value is printed with interpolation given inside the string.

    For more information: click here.


    51) Explain substr function in Perl?

    The substr function is used to truncate the string. The string will be truncated to offset value we provide.

    For more information: click here.


    52) How to compare two strings in Perl?

    To compare two strings in Perl eq is used instead of (==). It checks whether two strings are equal or not.

    For more information: click here.


    53) How to determine strings length in Perl?

    String length can be determined with length() function.

    For more information: click here.


    54) How to print escaping characters inside a string in Perl?

    Escaping characters are the special characters like @, \, /, &, $, ", etc. To print escaping characters put a backslash (\) before escaping characters.

    For more information: click here.


    55) What is qq (double q)operator in Perl?

    The qq operator replaces double quote surrounding a string by its parentheses. You can use qq instead of ("").

    For more information: click here.


    56) What is q (single q) operator in Perl?

    The q operator replaces single quote surrounding a string by its parentheses. You can use q instead of (').

    For more information: click here.


    57) What is STDIN in Perl?

    The STDIN stands for standard input. Using this input, we can get input from the standard console. It can be abbreviated as <>.

    For more information: click here.


    58) What is goto statement in Perl?

    The Perl goto statement is the jump statement. It transfers control by jumping to another label inside a loop.

    There are three goto forms:

    • goto LABEL
    • goto EXPR
    • goto &NAME

    For more information: click here.


    59) How to do comment in Perl?

    Like other languages, Perl also provides comment facility in its code. There are a single line and multi-line comment.

    For single line comment: use # before the line you want to comment.

    For multi-line comment: use =begin and =cut statement before and after the lines respectively you want to comment.

    For more information: click here.


    60) Explain regular expression in Perl?

    A regular expression is a string of characters that defines a specific pattern.

    There are three regular expression operators inside Perl:

    • Matching regular expression operator, m//
    • Substitute regular expression operator, s///
    • Transliterate regular expression operator, tr///

    For more information: click here.


    61) Explain split function in Perl?

    Perl split function splits a string at specified delimiter pattern like -, /,:, etc. By default, whitespace is assumed as delimiter pattern if nothing is specified.

    For more information: click here.


    62) Explain join function in Perl?

    Perl joins function joins symbol or character in between or afterward each element of an array.

    For more information: click here.


    63) Explain subroutine in Perl?

    Perl subroutine lets you reuse code in your program. They accept arguments, perform their operation and return the values. A subroutine is declared with 'sub' keyword before its name. In Perl, function and subroutine are used interchangeably.

    For more information: click here.


    64) How to access parameters passed to a subroutine in Perl?

    Parameters are accessed inside a subroutine using special array @_. Hence, the arguments will start with $_[0], $_[1], $_[2], $_[3] and so on.

    For more information: click here.


    65) Explain the use of 'my' keyword in Perl?

    The 'my' keyword restricts a variable to a particular region in which it can be used and accessed. Outside this region, this variable can't be used.

    For more information: click here.


    66) Explain the difference between declarations of 'my' and 'local' variable scope in Perl?

    Variables declared with 'my' keyword live within a code block and can't get its visibility inherited in the functions called within that block.

    Variables declared with 'local' keyword live within a code block and got its visibility in the functions called within that block.


    67) Explain the default scope of variables in Perl?

    By default, all variables in Perl are global variables unless they are locally defined. They can be accessed from anywhere in a program.

    For more information: click here.


    68) What is lexical variable in Perl?

    Lexical variables are created using 'my' keyword in Perl. They are private variables.


    69) How will you create a file in Perl?

    To create a file in Perl, '>' sign is used before the file name. It will create a new file.

    For more information: click here.


    70) How will you open a file in read-only mode in Perl?

    To open a file in read-only mode, '<' sign is used. You can only read this file and can't write anything.

    For more information: click here.


    71) How will you open a file in a write-only mode in Perl?

    To open a file in write-only mode, '>' sign is used. The file you open will be emptied or truncated if it already exists if not, a new file will be created.

    For more information: click here.


    72) How to prevent file truncation in Perl?

    Opening a file in write-only mode truncates data of the file. To prevent it, use sign '+>'. It will prevent your data, and you can append new data in the last of the file.

    For more information: click here.


    73) What is the use of '>>' in Perl?

    The '>>' sign opens a file with appending purpose. It places the pointer at the end of the file where you can add new data.

    For more information: click here.


    74) How to read a single line from a file in Perl?

    Taking $row = <$fh> as a variable will print a single line from the file.

    For more information: click here.


    75) How to read multi lines from a file in Perl?

    Taking $row = <$fh> as a variable in a while loop will print all lines from the file.

    For more information: click here.


    76) How to close a file in Perl?

    Closing a file in Perl is not mandatory. However, using close() function will disassociate the file handle from the corresponding file.

    For more information: click here.


    77) How to copy a file in Perl?

    To copy the content of one file into another file, read all lines of the first file in a while loop and copy it in another file.

    For more information: click here.


    78) Explain '->' in Perl?

    It is a symbolic link which links one file name to a new file name.

    For example, in file1 -> file2, if we read file1, we will end up reading file2.


    79) Explain a tell function in Perl?

    The tell function finds your position within a file. It is the first thing you need to do during file handling.


    80) What does file test operators do in Perl?

    File test operators check miscellaneous information about a file, for example, the type of file, file byte size, its UID or GID, etc.

    For more information: click here.


    81) How to open a directory in Perl?

    The opendir function is used to open a directory in Perl. It returns true on success or false on failure.


    82) How to create a directory in Perl?

    The mkdir function is used to create a directory in Perl. You need a require permission to create a directory.


    83) How to read a directory in Perl?

    The readdir function is used to read a directory in Perl. In scalar context, it will return each item one by one. In list context, it will return all the content of the directory in one statement. So list context uses more memory than scalar context.


    84) How to remove a directory in Perl?

    The rmdir function is used to remove a directory in Perl. The directory which you want to remove should be empty before removing it.


    85) How to change a directory in Perl?

    The chdir function is used to change a directory in Perl. To change a directory and go inside a new directory you need required permission.


    86) How to close a directory in Perl?

    The closedir function is used to close a directory in Perl. This function officially shut down the connection between directory handle and directory.


    87) What is a chop() function in Perl?

    Perl chop() function removes the last character from a string regardless of what that character is. It returns the chopped character.

    For more information: click here.


    88) What is a chomp() function in Perl?

    Perl chomp() function removes any newline character from the end of the string. It returns the number of characters removed from the string.

    For more information: click here.


    89) What does a die() function do in Perl?

    Perl die() function gives us a proper error message. It immediately terminates the script on encountering an error.

    For more information: click here.


    90) Explain the difference between die and exit in Perl?

    The die function prints the standard error message then exits the program. Whereas, the exit function terminates the program without giving any error message.


    91) What $! in Perl?

    The $! is a built-in error reporting variable in Perl. It tells us the reason for the error and prints it. It prints what the operating system tells it.

    For more information: click here.


    92) What is warn function in Perl?

    The warn function gives the warning on encountering an error but does not exit the script. The Script keeps on running.

    For more information: click here.


    93) What is confess function in Perl?

    The confess function is used within the Carp standard library for error handling.

    For more information: click here.


    94) What is eval function in Perl?

    The eval function is a built-in function in Perl which is used to detect the normal fatal error. It is supplied with a code block instead of passing into a string.

    For more information: click here.


    95) What is Perl DBI?

    DBI stands for Database Independent Interface. In Perl, the database is accessed using DBI module. It is a third party module provided by CPAN. It supports all the major database systems. It provides an abstraction layer between Perl code and database.

    For more information: click here.


    96) What does 'do' statement do in Perl?

    The "do statement" is used in Perl database. It is a shortcut to perform all the CRUD operation. If it is succeeded, it returns true, otherwise false.

    For more information: click here.


    97) What is 'commit' command in Perl?

    Once you have given Perl commit command, you can't retrieve back the changes you made.

    Syntax:

    For more information: click here.


    98) What is 'rollback' command in Perl?

    If you want to revert the changes made during the operation, call rollback command.

    Syntax:

    For more information: click here.


    99) What is automatic error handling in Perl?

    By specifying RaiseError option, your errors will be handled automatically by Perl. Your program will be aborted on encountering an error rather than running a failure code. Its value can be either 1 or 0.

    For more information: click here.


    100) What are some common methods to all handles in Perl?

    • err
    • errstr
    • trace
    • rows

    For more information: click here.


    101) What does localtime() do in Perl?

    The localtime() function is used without any argument, returns the current date and time according to the system.

    For more information: click here.


    102) What is the use of now constructor in Perl?

    The now constructor creates DateTime object displaying the current date and time using DateTime module.

    For more information: click here.


    103) Explain gmtime() function in Perl?

    This function works similar to localtime() with only one difference that returned value is localized for the standard Greenwich time zone.

    For more information: click here.


    104) What is epoch time in Perl?

    The epoch time refers to the number of seconds after a specific date and time. This specific date and time vary for different operating systems. For example, for Unix, it is January 1, 1970.

    For more information: click here.


    105) What is POSIX in Perl?

    POSIX - Perl interface to IEEE Std 1003.1

    The POSIX module allows you to access all the standard POSIX identifiers. This module provides much more functions than any other module.


    106) Explain strftime() function in Perl?

    The Perl POSIX strftime() function is used to format date and time with the specifiers preceded with (%) sign.

    For more information: click here.


    107) Explain socket programming in Perl?

    The socket is a procedure which helps to establish a virtual connection between different processes over a network.

    In socket programming, client and server-side script are made which communicate through each other over TCP/IP protocol.

    For more information: click here.



    Help Others, Please Share

    facebook twitter google plus pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA