Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Need Hel??

?   

             
      
Hello Friends, Who would have time to answer the following questions for
an interview prepration? Answer the one you feel like , I am trying to
collect different answers. 
1. What arguments do you frequently use for the Perl
interpreter and w 
+hat do they mean?
2. What does the command µuse strict¶ do and why should you
use it? 
3. What do the symbols $ @ and % mean when prefixing a c
variable?
4. What elements of the Perl language could you use to
structure your 
+code to allow for maximum re-use and maximum readability? c
5. What are the characteristics of a project that is well
suited to Pe
+rl? 
c
1. Why do you program in Perl?
2. Explain the difference between my and local.
3. Explain the difference between use and require. 
4. What¶s your favorite module and why? c
5. What is a hash?
6. Write a simple (common) regular expression to match an IP
address, 
+e-mail address, city-state-zipcode combination.
7. What purpose does each of the following serve: -w,
strict, -T ?
8. What is the difference between for&foreach, exec &
system?
9. Where do you go for Perl help?
10. Name an instance where you used a CPAN module.
11. How do you open a file for writing?
12. How would you replace a char in string and how do you
13. store th
+e number of replacements?
14. When would you not use Perl for a project?
[download]
Thanks

20050404 Unconsidered by Corion: Was considered by gmax: EDIT - remove code tags
and reparent under Interview Questions (edit:20 keep:6 del:2)

    
    

   
Re: Interview Prepration
by cog on Apr 03, 2005 at 20:07 UTC
I'll try... [reply]
[d/l]
 What arguments do you frequently use for the Perl [select]
interpreter and what do they mean?

Man... I use lots of them!!! I can't explain them all :-) I


even gave a talk and wrote an award winning paper on
them... But OK, I guess I'd go with -e for one-liners -n and -p
for line processing, and probably with -i for inline processing
(I would mention -w, but I "use warnings" instead)

x What does the command µuse strict¶ do and why should


you use it?

It enforces strictness! :-) You should use it because it will


help you avoid pestering errors that nobody likes (forgotten
my declarations being the most usual ones). I nowadays use
it most for discipline. It's not that one my programs wouldn't
work without it, but I prefer to be on the safe side. Also note
that I don't use strict for one-liners.

 What do the symbols $ @ and % mean when prefixing a


variable?

They mean what's after them is supposed to be interpreted,


respectively, as a scalar, an array or a hash.

 What elements of the Perl language could you use to


structure your code to allow for maximum re-use and
maximum readability?

Are you refering to modules or am I getting something


wrong? Are you talking about objects? Some good practices?
I think the question should be rephrased...

 What are the characteristics of a project that is well suited


to Perl?

It's not the project that is well suited, it's the language that
might be! And I don't feel confortable yet (with the current
experience I have) answering to that :-) Sorry :-)

Second round :-)

 Why do you program in Perl?

Because it's fun and fast (to code). I don't spend loads of
time trying to figure why the code doesn't work when I could
be spending it thinking about how to solve the problem. The
community around it is also a big plus!

x Explain the difference between my and local.

I'm not sure but I think I heard someone say it was kind of
the same thing, internally... I may be wrong about this... But
you do get the same effect, apart from some weird case I'm
not thinking about right now...

 Explain the difference between use and require.

"use" is at compile time, "require" is at run-time? It's weird


how you code and code and only when somebody asks you
something you start having questions about it :-)

 What¶s your favorite module and why?

CPAN::Mini, for now I have the CPAN on my laptop and no


longer rely on internet access to install new modules. If you
asked me for another one, it would be Data::Dumper

 What is a hash?

Look... that is not a Perl question... but OK, I'll play along...
Think of it as a table with two columns. You put keys in the
left column and their corresponding values in the right one.
And then I should tell you lots of stuff, like "You can't have
a value with a key", "You can't have two values for the same
key" and so on (No, you can't have two values for the same
key, what you can have is a value that holds more stuff
inside it).

 Write a simple (common) regular expression to match an


IP address, e-mail address, city-state-zipcode combination.

IP address: qr/(\d{1,3}\.){3}\d{1,3}/ # Yes, it accepts 0's at


the beginning and that's probably wrong, but then again, I'm
not actually at a job interview :-)

e-mail address: qr/\w+\@\w+\.\w+/ # once again, that's


wrong; I would have to check on what's valid and what's not,
because I really don't know :-)

city-state-zipcode: Being Portuguese, I really do not know


what a city-state-zipcode combination looks like :-\
 What purpose does each of the following serve: -w, strict,
-T ?

-w uses warnings, strict has already been explained above


and -T is tainted mode, which I usually don't use because I
don't usually get information I don't trust.

 What is the difference between for&foreach, exec &


system?

for&foreach are now an alias to each other (Ok, one of them


is implemented, but they're actually the same thing). As for
exec & system, I don't use them much, so I'd have to check
on documentation (and don't forget backticks, too)

 Where do you go for Perl help?

I usually *give* Perl help, but when I need it (because I


sometimes do too) I first ask some friends of mine, and then
I come to perlmonks.

 Name an instance where you used a CPAN module.

I use CPAN modules on a daily basis :-) I used LWP the


other day to retrieve my yml files (kwalitee stuff)

 How do you open a file for writing?

Usually with open (FILE, ">$file") or die "could


not open file '$file' for writing ($!)";

x How would you replace a char in string and how do you

$string =~ y/a/b/

 store the number of replacements?

my $number = y/blah/blah/, but that's not really a good


question, because many people don't know that, and that
doesn't make them worse programmers.

 When would you not use Perl for a project?

When Hell freezes over :-P


Re^2: Interview Prepration
by chas on Apr 03, 2005 at 20:44 UTC
"x 
        

 
    
   


   
     
 "
There is actually a significant difference. "my"
creates lexically scoped variables (e.g. existing
within a block.) "local" variables have run-time
scoping; "local" saves the (previously defined) values
of arguments on a stack and later restores them, using
the "locally defined" values during some containing
scope. Changes made by "local" to global variables
can be visible outside the lexical scope of the "local"
variables e.g. in nested subroutine calls. I learned
about this from the book "Effective Perl
Programming" by Joseph Hall, and Randal Schwartz
(Addison Wesley Publ.) Highly recommended!
chas [reply]
Re^3: Interview Prepration
by cog on Apr 03, 2005 at 20:47 UTC

 
     
 
 

I'm in the middle of a bunch of Perl books (I


always am).

Coincidentally, that's one of them :-) Guess I


didn't reach that part yet... :-) [reply]
Re^4: Interview Prepration
by chas on Apr 03, 2005 at 20:56 UTC
Re^2: Interview Prepration
by tilly on Apr 04, 2005 at 01:49 UTC
Round 1, question 3, your answer cannot be
reconciled with the syntax @foo{'this', 'that'} [reply]
= qw(whatever fits); [d/l]
Re^2: Interview Prepration
by jhourcle on Apr 04, 2005 at 03:17 UTC
½ 
         

 

 
    
 !
 "    
 

    
   


   


use also calls import(). From use: [reply]


[d/l]
Imports some semantics into the current [select]
package from the named module, generally
by aliasing certain subroutine or variable
names into your package. It is exactly
equivalent to

ËEGIN { require Module; import Module


LIST; }

D #
       
$ 
   $ "

$ 
 
  



  
%&        


 
   ' ?   $
       

    
 %
    

'

for can be used as an alias for foreach, and visa-


versa. But please, please don't use foreach with a C
style for loop:

foreach ( loop_init(); exit_test();


increment() ) { ... }

As for exec and system, one of 'em forks, one


doesn't. I always forgot which one is which, and look
them up, too. (exec never returns, while systemdoes)
and sometimes, I just open a pipeline.
Re^2: Interview Prepration
by merlyn on Apr 04, 2005 at 07:17 UTC
$ 
 
  



  
Uh, no. You might want to read what I wrote about
Perl's "contractions", and my specific comments on
"for" vs "foreach".

-- Randal L. Schwartz, Perl hacker


Be sure to read my standard disclaimer if this is a
reply. [reply]
Re^3: Interview Prepration
by Anonymous Monk on Apr 04, 2005 at 07:43 UTC
Uh, yes. The words for &foreach are an alias
to each other. [reply]
Re^4: Interview Prepration
by merlyn on Apr 04, 2005 at 17:28 UTC
Re^5: Interview Prepration
by Tanktalus on Apr 04, 2005 at 17:45 UTC
>      
 
     
Re^3: Interview Prepration
by cog on Apr 04, 2005 at 09:05 UTC
I know what you mean, you are correct, but
you can write for or foreach and you will get
the same result (and you say that in that link),
which was what I was trying to say :-)

It didn't even occured to me mentioning the


two different types of for/foreach because I
thought of the question in terms I answered it
:-) [reply]
Re^2: Interview Prepration
by zentara on Apr 04, 2005 at 16:17 UTC
"use" is at compile time, "require" is at run-time?
It's weird how you code and code and only when
somebody asks you something you start having
questions about it :-)

I'm a big believer in the saying: "The way to learn


something, is to teach it".

We ( I at least ) seem to have this "active" and


"passive" code knowledge; much like an "active and
passive" vocabulary. You don't really get to a real
understanding of it, until you have to explain it to
someone else ( who is asking WHY? alot ) :-).

I'm not really a human, but I play one on earth. flash


japh [reply]
Re: Interview Prepration
by cog on Apr 03, 2005 at 20:32 UTC
As much fun as answering this kind of questions is, I'd like
to note that you shouldn't rely an interview just on technical
issues. You shouldn't start by asking Perl questions and
choose the candidate with more knowledge (and that is also
because the guy might be just nervous).

Other things are also important... can you rely on the guy to
spend some time with people who need his help? can you
rely on him/her working harder when the deadline [reply]
approaches? Will he/she be a nice person to have around? :-)

All those things count :-)


Re: Interview Prepration
by CountZero on Apr 03, 2005 at 22:02 UTC
The e-mail regex.

But if the applicant for the job knows this regex by heart, I
wouldn't hire him. There must be something very
fundamentally wrong with him/her if he stuffs his brain with
this kind of knowledge.

CountZero

 
   
  

(!
   - Conway's Law [reply]
Re^2: Interview Prepration
by Fletch on Apr 04, 2005 at 02:32 UTC
Yeah, but if you ever need to know how many
toothpicks there are or when Judge Wapner is on . . .

Just don't let them drive. Yeah. [reply]


Re: Interview Prepration
by BUU on Apr 03, 2005 at 22:59 UTC
6. Write a simple (common) regular expression to
match an IP address, e-mail addr +ess, city-state-
zipcode combination.
You realise those regexes are neither simple nor common,
but instead tend to be really fairly difficult, especially if you
had to write one off the top of your head in a somewhat
stressful situation, such as an interview.

As for the rest of your questions, what is the point of asking


questions that could be answered in less than 10 seconds
with access to perldoc? Are you just trying to quiz them on
what knowledge they have memorized?

As for the rest of your questions, about what is suitable for


perl, presumbably you're already doing a project in perl, and
wish to hire someone to work on this project, so you know
what perl is suitable for, because you've chosen it as a
language already! What is the point of asking if he thinks it's
suitable? [reply]
Re: Interview Prepration
by Cody Pendant on Apr 04, 2005 at 01:20 UTC
Those questions are a strange mixture of absolutely
elementary and far more complex.

My answer to "6. Write a simple (common) regular


expression to match an IP address etc" would be "that's not
as simple as it sounds. I want to use a module". How would
that score?

I think these questions will just betray to the interviewees


that the person interviewing them doesn't know much about
Perl. Perhaps you'd be better off trying to find some Perl
guru local to you who could help out by attending the
interviews?

($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print [reply]
Re: Interview Prepration
by talexb on Apr 04, 2005 at 03:37 UTC
Instead of being horribly methodical, I just thought for my
own amusement I'd answer this post without checking
anything with the reference books. So if there are mistakes, it
has to do with my own faulty memory.

And in any case, a code sample should prove most of these


interview questions moot.

J #

         
    

  
"

I use -w to turn warnings on, and I use -c to compile code. I


also use -d to invoke the debugger. I never use perl without
arguments. That's like driving a car without a seatbelt, or
riding a motorcycle without a helmet.

x #
   
  
 
   "

use strict tells the Perl compiler to be a little pickier about


certain syntactical checking. It's almost never a poor choice.

½ #
  )*
+
 
  


 " [reply]
[d/l]
The dollar sign signifies a scalar, the at sign means an array, [select]
and the % means a hash, also known as an associative array.

( #
      

   
  
 
 ! 




"

I'm afraid I don't understand the question. I already use Perl


for 95% of my programming, with the reminder in some
shell scripting. (I didn't include SQL answering this question,
since it's not procedural when I use it.)

Structuring code has nothing to do with Perl, anyway. It's


basic software craftmanship.

, #

  

  
- 

   "

Perl is useful when you want to be able to leverage the work


of thousands of other dedicated, excellent programmers
available through CPAN modules, when you want to be able
to rapidly prototype, when you want to avoid paying
exorbitant prices for 'development environments', when you
want to avoid idiotic vendor limitations, useless support and
incomprehensible documentation.

J #  
  "

Someone once said that "Perl fits your brain like a glove" --
I'm able to get things done in Perl faster then I ever did using
C or awk, and getting things done is how I earn my salary. In
addition, there's a terrific Perl community available for
questions and support.

x 
        



my Creates a new lexical in the current scope; if there is an


existing variable with the same name, you'll get a
warning. local does the same thing but 'pushes' the original
value (if it exists) into the background until the current scope
is exhausted.

½ 
         
 

use brings the code in at compile time, where rqeuire pulls


the code in at run time.
( #

   
 "

I don't really have favourite module -- the one that handles


file names and directories is handy, File Special Functions or
something like that.

, #


 "

A hash is an unordered associative array, which means it's a


variable that contains multiple elements, where each element
is accessed by an associated key, rather than by an offset
(like in an array).

. # 
 % ' 
   

 

  !

 !
 !
/ 


This question is vague, and I don't have time to prove why.

 #
   
       !
!0"

You already asked about -w -- that's Perl's command line


option for warnings. You already asken about strict --
you use strict when you want the perl compiler to do
additional syntax checking. The -T command line switch
turns on taint checking, something that's useful for CGIs
when you want to make sure there's no user input that can
possibly flow through to the operating system for security
reasons. Typically you want to enable taint-checking as early
in the command line options as possible.

D #
        $ 
   
$ "

for in Perl is like a for loop in C; foreach iterates over a


list. exec calls an external program, never to return,
where system calls the external program and returns.

r #      "

I look in perldoc, in the various O'Reilly books that I have,


and finally I visit Perl Monks.

J 1
 
 
    
2?1
 
When I wanted to write a command line utility to log on to a
web application through HTTPS and upload a file, I
used LWP::Simple. This saved me huge amounts of time and
sweat.

JJ 3  
    "

I use a '>' in the open statement. I also check that the open
succeeded, and if not, the script dies.

Jx 3  
 

  

I could use s/// or tr/// to replace a character in a string,


but it depends on the situation.

  J½       


  "

I think tr/// returns the number of replacements, but I'd


have to check.

J( #      


- "

When either shell scripting will do the job, or when


performance is paramount in which case I'd look at C. There
may also be other external factors.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus


Torvalds
Re: Interview Prepration
by tilly on Apr 04, 2005 at 03:48 UTC
Answers to these are not going to help you. But I'll fire these
off since I'm feeling bored.

Round 1.

1.c #

         
    

  
" Whatever ones
are appropriate and they mean whatperlrun says they
mean.
2.c #
   
4 5 
 
   " Both an explanation of what it [reply]
does and why it is good may be found atstrict.pm. I [d/l]
refer to it as a typo checker. [select]
3.c #
  )*
+
 
  


 " The way that I think about it is
that $ means "the", @ means "these", and % means
"dereference as a hash". Note that if you are
accessing a single element of an array or a hash then
you use $ for "the" element, and if you take a slice of
an array or a hash then you use @ for "these" items
from the slice.
4.c #
      

   
  
 
 ! 




" The whole language. I really
hope that you're planning to fail someone who
blathers out, "Its support for object-oriented
programming" because if you think that OO is the
only path to reusability and maintainability, then
you're not very good at either of those.

Round 2.

5.c #  
  " Because I'm paid and I
enjoy being productive.
6.c 
        


 The local operator is misnamed, it really means
something like "temp". It temporarily replaces a
variable with a new variable, and then when you
finally exit your current scope it replaces the old
value. (If you call other functions in other scopes, the
locally set variable is visible.) By contrast my creates
a lexical scope, any references to that variable from
the declaration to the brace that ends your scope
refers to the new lexical variable. To underscore that
the two mechanisms are unrelated, note that it is
possible to use local on variables in hashes and
arrays. The only reason that you cannot use local on
lexical variables is that Larry Wall thinks that that
would be too confusing.
7.c 
         

  When you say use Foo qw(bar
baz); you're saying ËEGIN {require Foo; Foo-
>import(qw(bar baz));}. (In an interview I could
explain that further.)
8.c #
5
   
 " There are too
many out there to have a single favorite, but if I had
to name one then it would probably be DBI. In my
work I encounter a lot of databases, and a database
driver/interface is necessary piece that I use all of the
time that I'm glad not to have to write.
9.c #


 " A hash is a kind of datastructure for
key/value pairs that makes average time to access a
key, insert a key, or delete a key O(1). If need be I
can explain how they work. They are a native data
type in Perl. In Perl you use %foo to talk about the
whole hash, and $foo{bar} to access one value in
the hash. A coding tip. I like naming my hashes in
such a way that hash lookups can be read "of".
10.c# 
 % ' 
   

 

  !

 !
 !
/ 
 I would not handle these tasks
with a single regular expression, and I would advise
not hiring anyone who thinks that they can do so.
11.c#
   
       !
!0" They turn on warnings, make Perl less
lenient, and disallow doing dangerous operations
with dangerous data.
12.c#
        $ 
   $
 " Well for and foreach are synonyms. You're
probably thinking of the difference between the C-
style for (my $i = 0; $i < 10; $i++)
{...} and the clearer Perlish foreach my $i
(0..9) {...}. As for the other two, exec replaces
the current process with another, while system pauses
the current program as it runs another one, then
returns information about whether that program
succeeded.
13.c#      " First perldoc. Then
my library of books. Then co-workers. Then
perlmonks. In that order.
14.c1
 
 
    
2?1
  Do I get bonus marks for using my own
CPAN module? :-P
15.c3  
    " Something like
this: open(my $fh, ">", $file) or die "Cannot
write to '$file': $!"; Note that the error
message matches the recommendations in perlstyle,
and note the use of 3-argument open as discussed
at Two-arg open() considered dangerous.
16.c3  
 

  " $string
=~ s/$char/$replacement/g;
17.c3       
  " my
$count = ($string =~
s/$char/$replacement/g); Note that the parens
are not required, but I think it is clearer with them
included.</code>
18.c#      
- " There are
lots of possible reasons not to use Perl. Performance
really is critical. Space really is critical. The project
is to adapt a program written in some other language.
Key developers really hate Perl and the political
battle is not worthwhile. Possible reasons multiply ad
nauseum.

Re: Interview Prepration


by theorbtwo on Apr 04, 2005 at 06:32 UTC
Well, I scanned through the other answers somewhat, but
didn't really read them, and decided that it'd be good pratice
to answer them, so here we go...

1.c Well, the most common one I use is -e -- for putting a


short one-liner on the command-line. Almost as
often, I use -l -- makes print append a "\n"
automatically. (Somewhat annoyingly, -l does not
effectprintf.) Also, -M, which is shorthand for use,
very useful with -MO=Deparse,-p. Less often, but
still rather common, is -I, shorthand for putting a dir
in PERL5LIB / @INC. (Somewhat different
from use lib.) Also, -p and -n are sometimes useful
-- they are shorthand for
2.c while (<>)<code> loops, both with and
without printing $_.</li>
3.c <li>They give the sort of thing returned by
the variable expression -
4.c +- scalar, list, or hashy list. (Note that
the obvious -- they are pa
5.c +rt of the name, giving the type of
variable -- is slightly wrong in p
6.c +erl5, but correct for perl6. The
difference is <code>$foo[1]
[download]

vs @foo[1].

7.c Modules and subroutines, including in some cases


OO. Also, POD and comments. Also, refs, and in
purticular code refs.
8.c This is always a difficult question, for any language.
Somewhat easier to answer is "what are
characteristics of a project that make it poorly suited
to Perl". One is if the project is already largely [reply]
complete in another language, and the completed part [d/l]
is not easily wrappable in a library. Another is if the [select]
people who should be working on the project do not
know perl, and the other languages they know are
decently suited to the task -- even if perl is better for
the project, it may not be worth the learning curve for
this project.
9.c I program in Perl because it lets me focus on the bits
of the problem that I wish to. I don't have to manually
handle memory management; the runtime does that
for me if I follow a few rules, which I very rarely
have reason to wish to not follow. There are many
free and high-quality libraries available -- I don't
have to code my own XML parser, or my own
database interface. The perl community is highly
supportive -- if I want help and advice, I know where
I can go to get it quickly, and with a low occourance
of bad advice. Oh, and there is very little that is
impossible to do in perl, and when there is such a
thing, I can program just a bit in C, and the rest in
perl, quite easily, by using Inline.
10.cmy uses lexical scoping: the variable is visible from
the statement after the my until the end of the block
that encloses the my statement. This means that you
know where the variable will and won't be visible
from just a glance at the code -- a great improvement
from dynamic scoping, which is what local uses. In
dynamic scoping, the variable is visible from the line
following the local statement to the end of the
enclosing block, and any code called from there, and
any code called from code called from there, etc.
This means that there's no telling where it will show
up, and where it can be modified, encourging code
that is difficult to keep straight in your head, and
where modifications to one piece of code may change
the behavior of other code that, at first glance,
appears completely unrelated. Additionally,
dynamicly scoped variables are slightly slower to
access.
11.cUse takes place at compile time, require takes place
at runtime. Use automatically calls the sub named
import in the module being used (unless the use
statement is followed by a pair of empty parens),
require does not call the import sub. Because of this,
if you expect the module to do setup at use time, or
export semantics (including subroutines and
pragmatic behaviors), then use "use". If you want it
to be effected by the flow of the program, use
require. Otherwise, it's a matter of taste -- most
people use use, mostly because it's slightly shorter.
12.cI'd have to go with strict. It makes me more
efficent by catching many of my errors early, and
making them easy to find. Not only that, but it
encourages me to think about my scoping, leading
me to produce better code. I use strict in almost every
piece of perl I write, and when I don't, I often end up
regretting it.
13.cA hash is a mapping from string keys to scalar
values. The mapping is unique and unordered.
Dispite this rather humble defintion, it has an
amazing power to create powerful and elegant code;
easy-to-use hashes are one of the simplest reason that
Perl is such a wonderful language. Many other
languages have hashes, but they tend to be
implemented as a class of object, which means that
the syntax required to access them gets in the way of
what you're actually interested in when reading and
writing code -- the problem you are attempting to
slove.
14.cWell, none of those problems are
purticularlyammenable to writing simple regexes that
match everything they should and nothing they
should not. Fortunately, there is a module,
Regexp::Common, that already has regexes for
matching IPs and email addresses. The problem with
IPs is that they require numeric operations to match
correctly; not all three-digit numbers are valid octets.
Not only that, but 10.1 is a valid IP address -- it is an
uncommonly used, but quite legal, shorthand for
10.0.0.1. As for email addresses, while ;/[; ]@[a-
zA-Z0-9-.]+$/ will match  email addresses, it
will not match
 email addresses. It will not match
the forms that include comments, nor will it match
the @-less form (IE ones that use the default domain
name of the server). It will, however, catch many
things that are not valid email addresses -- those that
include a domain name that does not actually exist,
for example. (Many other common regexes for email
addresses will fail more -- for example, many people
do not realize that + is both a valid character for the
username part, and a rather common one. Many
people do not realize that some addresses at registrars
have names like noc@cc, with no .in the domain-
name part.) city-state-zip is complex because the
comma is sometimes missing between city and state
(in fact, the USPS prefers that it not be there), and the
state may be written as a two-character code, as the
correct state name, or as an incorrect-but-common
abbreviation. The ZIP code may be not present, 5
digits, or 5+4. For that matter, attempting to match
against a regex will miss mailing addresses from
outside of the US, fustrating users and loosing
customers.
15.c-w enables warnings globally for the perl interpreter.
It can be used for code that needs to maintain
compatability with 5.005 and older perls, but for
newer code, use warnings is better, because it will
not enable warnings in parts of the program that are
not designed to be run with them enabled. Strict I
mentioned previously, as my favorite module. It
disables constructs that make for unmantaintable
code and increased time spent tracing avoidable
errors: implicitly global variables, bareword strings,
and symbolic references. Of course, individual
strictures can be turned off for the portions of code
that need them -- most useful for symbolic refs in the
very limited circumstances where they are useful. -T
is taint mode. It will catch certian classes of security
issues when dealing with untrusted user data.
However, -T is no replacement for thinking about
these issues explicitly -- it will allow some types of
things that should not be allowed, and will disallow
some things that are perfectly safe.
16.cThere is no difference between for and foreach;
they are simply alternate ways of writing the same
thing. Exec and system differ in that exec replaces
your code with some other executable, whereas
system creates a sepperate process. Exec will never
return (unless the exec failed); system will return.
17.cThat depends on what you mean by "go". If I need
perl documentation, perldoc, perldoc -f, man,
perldoc.perl.org, or search.cpan.org, depending
largely on my current environment and mood. If I
need to ask other humans a question, then
perlmonks.org -- either Seekers of Perl Wisdom for
written help, or the chatterbox for "spoken" help.
18.cI regularly use Finance::Bank::Norisbank -- in fact, I
use it exactly every two hours, from a cron job; it
keeps track of activity on my bank account. I write
code that uses HTML::TreeBuilder, LWP::Simple,
and XML::Simple quite often, essensially whenever I
want to do a repetitive task involving the web. I can
often write code that preforms a repetitive task faster
then doing it myself, and it is almost always more
interesting to do so.
19.cGenerally, with IO::Open->new($filename, ">")
or die "Couldn't open $filename for
writing: $!";. In some circumstances, of course,
death is not the right thing to do when the file can't be
opened, or I wish to append with >> rather then
overwrite with >, or I wish to open the file in utf8
mode with ">:utf8".
20.c$string =~ s/a/b/g
21.cmy $count = $string =~ s/a/b/g. Sometimes, I
use tr/// instead of s///; tr is more convient when there
is a mapping of multiple chars to multiple chars, and
the mapping is more easily expressed explicitly then
via code.
22.cI already answered this question

Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is
posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the
public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly
regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Interview Prepration


by Anonymous Monk on Apr 04, 2005 at 10:57 UTC
1.c Seldomly when using a she-bang line (-T being an
exception, and for quick and dirty hacking, -s), but
from the command-line: -w, -p, -n, -a, -i, -l, -e, -0, -c,
-D, -v, -V, -I, -M, -x belong to my favourites. Do I
have the time to explain all of them?
2.c use strict is not a command - it's a statement. It
compiles the strict.pm module, and calls
the import sub in it. This will twiddle some bits in
$^H, which causes the compiler to check for certain
things, and complain if the conditions aren't met. I
don't know whether I should use it, but I can explain
why I often do use it. (insert strictness mantra).
3.c It depends a little on the context. It could mean the
following variable are scalars, arrays or hashes, but it
could also mean that the following expression is used
as a scalar, list, array or hash.
4.c All of them. But perhaps "whitespace" is the most
important for readability (and hence, reusability).
Although, technically, "whitespace" would mean [reply]
the
  of any element of the Perl language. [d/l]
5.c I believe that "suitable language" is more defined by
the programmers going to do the project than the
project itself most of the time, so my answer would
be "if the programmers are Perl programmers".
6.c Because I happen to know it, and it often satisfies my
needs.
7.c my declares a lexical variable (or a set of lexical
variables), local creates a new (or a set of
new)
 for a non-lexical variable (which is not
the same as a package variable).
8.c use is done at compile time, calls import if
applicable, and can only be given a bare word as first
argument. require is done at run-time, takes an
expression (or a version) as argument, and doesn't
call import.
9.c It depends on what I want to do.
10.cA mapping that maps strings to arbitrary scalars.
11.cIP addresses? In which format? Dotted quads? Hex?
32 bit integers? IPv4? IPv6? There are no simple
regexes for e-mail addresses, unless you either wants
lots of false positives, false negatives, or both. City-
state-zipcode of which country? US? If so, what
format zip codes? 5 digits? 5+4? Either? As for
states, 2 letter states? 3 letter states? 2 or 3 letter
states? Do you expect your programmers to
remember all 50 abbreviations? There are a few extra
areas that have "state codes" as well? Am I supposed
to know them?
12.c-w tests whether a file (or filehandle) is writeable for
the EUID, -T guesses if a file (or filehandle) is an
ASCII file, and strict is a bare word that "use strict;"
wouldn't approve off.
13.cThere's no difference between for and foreach. And
system does a fork, execs in the child, waits in the
parent.
14.cThe manual has all I need.
15.cYou see, I wanted to write this program ....
16.cIt depends. There's 2-arg open, 3-arg open, sysopen
(either of which can be used with a filehandle, or a
reference to a filehandle as first argument, and Perl
will happelyautovivify an undefined value into a
reference to a filehandle for you as well), and
modules like IO::File, IO::All, and Inline::Files. I
typically use 2-arg open, 3-arg open and sometimes
sysopen. All three with references to filehandles
(usually autovivified handles) as first argument.
Filehandles as first argument is asking for trouble.
17.c_A_ character in a string? As in, a single one? Say,
the third character? substr($str, 2, 1, "x") if
length($str) >= 3;
18.cWell, that would be either 0 or 1, and it will be 1 if,
and only if, the string originally was at least 3
characters long.
19.cIf the other programmers don't know Perl. If speed is
crucial. If the target environment has limited
resources (memory, disk, CPU). If the source cannot
be distributed. If deployment is going to be a
problem. If portability demands it. If it needs to run
in an environment perl doesn't run in, or can't be
installed into. If the customer demands a .NET
solution. If there's a domain specific language far
more suitable for the problem than a generic
language like Perl. If low level memory access is a
major part of the solution. If the moon is blue. If I
feel like using Java for today. If it's better done using
a Makefile.

Re^2: Interview Prepration


by merlyn on Apr 04, 2005 at 17:36 UTC
0          



Ahem. Not this again. Please see what I just said,
elsewhere in this thread.

This meme must die. Even if I have to hunt down


every appearance of it. {sigh}

-- Randal L. Schwartz, Perl hacker


Be sure to read my standard disclaimer if this is a
reply. [reply]
Re^3: Interview Prepration
by chromatic on Apr 04, 2005 at 17:53 UTC
That's a really thin line to walk -- thin enough
that I think it's almost meaningless and
definitely more confusing than useful. Is this
a for loop?

foreach ( my $i = 0; $i < 10 ; $i++ )


{
print "\$i is $i\n"; [reply]
} [d/l]
[download] [select]
Is this a foreach loop?

for my $user (@names)


{
print "Found user $user\n";
}
[download]
Re^4: Interview Prepration
by merlyn on Apr 04, 2005 at 18:04 UTC
Re^5: Interview Prepration
by demerphq on Apr 05, 2005 at 17:03 UTC
>      
 
     
Re^3: Interview Prepration
by jZed on Apr 04, 2005 at 18:02 UTC
The difference between "for" and "foreach" is
four characters of extra typing for the latter.
Other than that they are the same. Because
you apparently associate one of the words
with a Perl-style loop and another with a C-
style loop is not sufficient reason to say that
"for" is different from "foreach". The words
may have a different history, but their use in
scripts is identical. [reply]
Re^4: Interview Prepration
by merlyn on Apr 04, 2005 at 18:08 UTC
Re^5: Interview Prepration
by jZed on Apr 04, 2005 at 18:10 UTC
>      
 
     
Re^3: Interview Prepration
by Anonymous Monk on Apr 05, 2005 at 08:48 UTC
Your   has been noted.
But perl doesn't care about your opinion, and
will treat the keywords for and foreach to
be the same. I'm just answering the interview
questions - and I think that the interview is
about probing my perl knowledge, and what I
know about the opinion of some well known
members of the Perl community. [reply]
Re^4: Interview Prepration
by RazorbladeBidet on Apr 05, 2005 at 12:36 UTC
Re: Interview Prepration
by Anonymous Monk on Apr 04, 2005 at 12:44 UTC
try to see if they have a good attitude towards learning and if [reply]
they will fit in in your company, instead.
Re: Interview Prepration
by prostoalex on Apr 04, 2005 at 19:28 UTC
It's actually a copy of Perl Interview Questions from
TechInterviews.com.

Judging by the Anonymous Monk's spelling pattern,


someone in India with no Perl knowledge desperately needs
a job, no? [reply]
Re^2: Interview Prepration
by ww on Apr 04, 2005 at 19:49 UTC
Actually, that jobseeker interpretation matches mine;
namely, that the OP was looking for the answers in
expectation of     
 as opposed to
"conducting an interview of a prospective employee."

However, there's something else peculiar here: the


bottom of the perl interview page you cite (and link)
says: "Check out Perl Monks for discussion of the
questions and related Perl interview issues. Check
RegExpLib for regular expression samples."
RegExpLib appears to be site which provides
advertising for regex-related software... but the
reference and link to Perl Monks raise (for me) the
further question of whether someone is using the
Monastery's good name as an implied endorsement of
a product?

I should note, tho the pm link is to the Gates, so I


suppose the reference and link could have existed
prior to the OP's initiating this thread.

      


   [reply]
Re^3: Interview Prepration
by merlyn on Apr 04, 2005 at 20:01 UTC
Ouch... they put The Monastery and that ugly
Regexlib in the same category? Regexlib is
cargo-culted after cargo-culted regex written
by amateur hacks, with very little attention
paid to standards and usability. Eeek.

-- Randal L. Schwartz, Perl hacker


Be sure to read my standard disclaimer if this
is a reply. [reply]
Re^4: Interview Prepration
by Tanktalus on Apr 04, 2005 at 20:39 UTC
Re: Interview Prepration
by chris7cmcc on Nov 07, 2006 at 18:56 UTC
A   

local = dynamic scoping:

localmy be used to limit the scope of a variable to a specific


subroutine and all subroutines called from it.

my = lexical scoping:

my defines a scope that is limited to a specific subroutine,


excluding those called from within it. [reply]
Back to Seekers of Perl Wisdom

PerlMonks lovingly hand-crafted by Tim Vroom.


PerlMonks went on a couple dates, and then decided to shack up with The Perl Foundation.
Beefy Boxes and Bandwidth Generously Provided by pair Networks
c

You might also like