- Home
- Interview Questions
- Perl
References in Perl point to scalars only. References to arrays point to the beginning of the array.
Arrays can contain references to other arrays, hashes, and so on. The way to create
multidimensional arrays in Perl is by using references to references.
The address printed out with the print statement on a reference has a qualifier word in front of it.
For example, a reference to a hash has the word HASH followed by an address value, an array has
the word ARRAY, and so on
Both *moo and $_main{'moo'}mean the same variable (as long as you aren't using a package).
*moo is more efficient because the reference is looked up once at compile time, whereas
$_main{'moo'}is evaluated at runtime and evaluated each time it is run.
To distinguish them from variables that you define and to ensure that the reset function cannot affect them.
The functions that use $_as the default are those that are likely to appear in Perl programs specified on the command line using the -e option
Without an explicit return, subroutines return the last value that was evaluated in the block.
Trick question! Return can be used to return either a scalar or a list, depending on what you want to return.
When you pass any list into a subroutine, it is flattened into a single list of scalar values. In the case of hashes, this means unwinding the hash into its component keys and values (the same way a hash is handled in any general list context).
@_ refers to the argument list of the current subroutine. It’s most commonly used inside a subroutine definition, although you can also define is as a global variable
Perl does not have formal named parameters. You can extract elements from the argument list @_ and assign them to local variables in the body of your subroutine (although once assigned to local values, they cease to be references to the same values outside that subroutine).