- Home
- Interview Questions
- Perl
Numbers, strings, and references. Full credit if you only said numbers and strings; we haven’t talked about references yet.
There are two differences between single- and double-quoted strings:
-> Double-quoted strings can contain any number of special character escapes;
single-quoted strings can only contain \' and \\.
-> Double-quoted strings will interpolate variables inside them (that is, replace
the variables with their values).
$count
$11foo
$_placeholder
$back2thefuture
$long_variable_to_hold_important_value_for_later
All the variables in that list except for $11foo are valid. $11foo is invalid because it starts with a number (Perl does have variables that start with numbers, but they're all reserved for use by Perl).
The = operator is the assignment operator, to assign a value to a variable. The == operator is for testing equality between numbers.
A statement is a single operation in Perl. An expression is a statement that returns a value; you can often nest several expressions inside a single Perl statement.
The die function exits the script gracefully with a (supposedly) helpful error message. It's most commonly used in conjunction with open because if something so unusual happened that the file couldn’t be opened, generally you don't want your script to continue running. Good programming practice says always check your return values for open and call die if it didn’t work.
Read input from a file handle using the input operator <> and the name of the file handle. In scalar context, the input operator reads one line at a time. Inside a while loop, it assigns each line to the $_ variable. In a list context, all the input to end of file is read.
Send output to a file handle using the print function and the name of the file handle. Note that there is no comma between the file handle and the thing to print.
The @ARGV array variable stores all the arguments and switches the script was called with.
The getopt function defines switches with values, and can accept any other options. The getopts declares the possible options for the script and if they have values or not. The other difference is that getopts returns a false value if there were errors processing the command-line switches; getopt doesn’t return any useful value.