- Home
- Interview Questions
- Perl
Operator precedence determines which parts of an expression are evaluated first given expressions that contain other expressions. Associativity determines the order in which operators that have the same precedence are evaluated.
/d/
/\d/
/\D/
/d\d/
The patterns are as follows:
-> /d/ matches the single character d.
-> /\d/ matches a single digit.
-> \/D/ matches a single nondigit character.
-> /d\d/ matches a d followed by a single digit.
'456'
'd55'
'55+'
'4aa'
'4b'
The pattern /\d\d\D/ matches two digits and one nondigit, in that order. None of the given strings match the pattern except for '55+'.
A file handle is used to read data from or write data to a source or a destination, be it a file, the keyboard, the screen, or some other device. File handles provide a common way for Perl to handle input and output with all those things.
The chomp function removes the newline from the end of a string. If there is no newline on the end of the string, chomp does nothing.
Hard links provide a way for a single file to have multiple filenames. Removing
one instance of the filename—be it the original or the link—leaves all other hard
links intact. The file is only actually removed when all the links to it are also removed.
Symbolic links also provide a way for a single file to have multiple names, except
that the original file is separate from the links. Removing the links has no effect on
the file; removing the original file makes any existing links point to nothing.
Note that only Unix systems differentiate between hard and symbolic links, and
some Unix systems don't even have symbolic links. Aliases on the Mac or shortcuts
on Windows are analogous to symbolic links.
The 'pwd' command makes use of back quotes and the pwd command in Unix. While some versions of Perl work around this function on other systems, for a more portable version use the Cwd module and the cwd() function. Note: you’ll have to import Cwd (use Cwd) before you can use it.
A file glob is a way of getting a list of files that match a certain pattern. *.pl, for example, is a glob that returns all the files in a directory with a .pl extension. Globs are useful for grabbing and reading a set of files without having to open the file handle, read the list, and process only those files that match a pattern.
A file glob of <*> returns all the files in a directory, but it does not include hidden files, files that start with a ., or the . and .. directories. Directory handles return all these things.
File handles are used to read and write the contents of files (or to the screen, or to a network connection, and so on). Directory handles are used to read lists of files from directories. Both file handles and directory handles use their own variables, and the names do not clash.