William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Perl interview

1. write a unix filter in perl (program that reads stdin and writes stdout)
that will reformat a multi-record, comma delimited file with 4 fields per line and select only fields 1,3,4 and output the fields in the order
4,1,3. Assume that the file is massive and cannot be processed in memory.

for example:
john@chtah.com,Doe,John,M becomes M,john@chtah.com,John

2. What is the use of the keyword ‘bless’ in perl?
A: bless $self, $type;

3. In order to delete a file on a unix filesystem:
what permissions are required on the file itself?
what permissions are required on the parent directory?

4. Given the database schema below, write an SQL query that returns a list of user email addresses with associated ‘fname’, where the
value of the interest field is 64.

+———————-+ +——————————–+
|table: USER | | table: PROFILE |
|———————-| +——————————–+
|email user_id perm | |user_id pref ic_ind |
+———————-+ +——————————–+

+—————————–+
|table: DEMOGRAPHIC: |
|—————————–+
|user_id fname lname interest |
+—————————–+

5. How does one direct output on file descriptor 4 to stderr in shell?
A: 4>>&2

6. How is memory attained and disposed of in C?

7. How is the character ‘*’ used to reference variables in C and Perl?

8. What operations (besides assignment) are performed below?
What are the result values in the variables $x and $y?

8a. my $x = 0 | 2 | 4;
8b. my $y = 0 || 2 || 4;

9. What is the result of each of the following expressions?

9a. my ($a,@b,$c) = qw( 1 2 3 4 5 );
A: $a=1; \@b = [‘2′,’3′,’4′,’5’]; $c=undefined
9b. my $a = (4,2,3,5,2);
B: $a=2;

10. write a single regular expression to validate and capture the digits
out of a phone number where area code is optional, and values are
separated by dashes, into 3 scalar variables.
A: m{(?:\b|)?([\d\-\(\)\.]{7,})(?:\b||\s)}s;

11. Given a perl script that is being sent a file of newline delimited
records on standard input, what is the result of the following
statements?

local $/ = undef;
my $var = ;

12. Explain the difference between a signed integer and an unsigned integer
unsidned integer = signed integer * 2;

Others:

1. What arguments do you frequently use for the Perl interpreter and what do they mean?
A: perl -d for debug; perl -wc for compiling.

2. What does the command ??use strict? do and why should you use it?
A: for strict definition, e.g: use my.

A command like use strict is called a pragma. Pragmas are instructions to the Perl interpreter to do something special when it runs your program. use strict does two things that make it harder to write bad software: It makes you declare all your variables (“strict vars”), and it makes it harder for Perl to mistake your intentions when you are using subs (“strict subs”).

3. What do the symbols $ @ and % mean when prefixing a variable?
A: $: scalar; @: array; %: hash

4. What elements of the Perl language could you use to structure your code to allow for maximum re-use and maximum readability?
A: package – modules.

5. What are the characteristics of a project that is well suited to Perl?
For writing exploits, testing products and apps, identifying new Web-based vulnerabilities, and creating complex regular-expression engines.

6. Why do you program in Perl?
A: easy use and high efficient in server-side; closely matched in Linux/Unix command-line; powerful in RE and array/Hash; compatible with awk,sed,grep and shell scripts.

7. Explain the difference between my and local.
A: both are local variable; local can be inherited in sub function, local defines some default variables in perl (local $/,$|,$^ etc).
The variables declared with my() are visible only within the scope of the block which names them.
They are not visible outside of this block, not even in routines or blocks that it calls.
local() variables, on the other hand, are visible to routines that are called from the block where they are declared.
Neither is visible after the end (the final closing curly brace) of the block at all.

local and my are different
local does not work the same way as my. In particular, it doesn’t create private variables.
Variables declared local remain global. Instead, Perl assigns a temporary value to the variable,
and then restores the old value when the variable goes out of scope.
Because variables declared as local are still global variables, there is another time where they are handy.
Remember that my variables are private, and cannot be seen by subroutines. This is not true when they are declared local.

8. Explain the difference between use and require.
A: require: running period, die if not passed; use: compiling period, warning if not passed;

require() reads a file containing Perl code and compiles it. Before attempting to load the file it looks up the argument in %INC to see
whether it has already been loaded. If it has, require() just returns without doing a thing.
Otherwise an attempt will be made to load and compile the file.

use(), just like require(), loads and compiles files containing Perl code, but it works with modules only and is executed at compile time.
The only way to pass a module to load is by its module name and not its filename. If the module is located in MyCode.pm, the correct way to use() it is:

Perl offers several different ways to include code from one file into another. Here are the differences between the various inclusion commands:

9. What’s your favorite module and why?
A: www::mechanize, cpan, xml::twig, dbi, cgi, html::template, mod_perl…

10. What is a hash?
A: %hash = (k=>v); $hash_ref = $(k=>v);
11. Write a simple (common) regular expression to match an IP address, e-mail address, city-state-zipcode combination.

12. What purpose does each of the following serve: -w, strict, -T ?

13. What is the difference between for & foreach, exec & system?
A: foreach: loop; map, grep use foreach inside.
exec = qx = “

14. Where do you go for Perl help?
A: perldoc, cpan and perl on-line html document

15. Name an instance where you used a CPAN module.
A: perl -MCPAN -i install apache

16. How do you open a file for writing?
A: open; use FileHandle;

17. How would you replace a char in string and how do you store the number of replacements?
A: ($number) = ($str=~s///g);

18. When would you not use Perl for a project?
A: web developing: use PHP instead; mathmetic: java;

and:
What arguments do you frequently use for the Perl interpreter and what do they mean?
perl [ -sTuU ] [ -hv ] [ -V[:configvar] ] [ -cw ] [ -d[:debugger] ] [ -D[number/list] ] [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal] ] [ -Idir ] [ -m[-]module ] [ -M[-]‘module…’ ] [ -P ] [ -S ] [ -x[dir] ] [ -i[extension] ] [ -e ‘command’ ] [ — ] [ programfile ] [ argument ]

Advertisements

2 responses to “Perl interview

  1. powercashadvance.com 10/28/2011 at 10:29 am

    I couldn’t have asked for a better blog. You are ever present to give excellent tips, going directly to the point for simple understanding of your website visitors. You’re really a terrific professional in this area. Many thanks for always being there for people like me.

  2. Chris 04/10/2013 at 4:19 pm

    When would you not use Perl for a project? A: web developing: use PHP instead.

    That made me laugh. Perl is an excellent language for web developing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: