Interview :: Perl
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///
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.
Explain join function in Perl?
Perl joins function joins symbol or character in between or afterward each element of an array.
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.
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.
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.
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.
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.
Lexical variables are created using 'my' keyword in Perl. They are private variables.
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.