http://qs321.pair.com?node_id=444520

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Interview Prepration
by tilly (Archbishop) 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. What arguments do you frequently use for the Perl interpreter and what do they mean? Whatever ones are appropriate and they mean what perlrun says they mean.
    2. What does the command ‘use strict’ do and why should you use it? Both an explanation of what it does and why it is good may be found at strict.pm. I refer to it as a typo checker.
    3. What do the symbols $ @ and % mean when prefixing a variable? 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. What elements of the Perl language could you use to structure your code to allow for maximum re-use and maximum readability? 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.
    1. Why do you program in Perl? Because I'm paid and I enjoy being productive.
    2. Explain the difference between my and local. 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.
    3. Explain the difference between use and require. When you say use Foo qw(bar baz); you're saying BEGIN {require Foo; Foo->import(qw(bar baz));}. (In an interview I could explain that further.)
    4. What’s your favorite module and why? 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.
    5. What is a hash? 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".
    6. Write a simple (common) regular expression to match an IP address, e-mail address, city-state-zipcode combination. 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.
    7. What purpose does each of the following serve: -w, strict, -T ? They turn on warnings, make Perl less lenient, and disallow doing dangerous operations with dangerous data.
    8. What is the difference between for & foreach, exec & system? 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.
    9. Where do you go for Perl help? First perldoc. Then my library of books. Then co-workers. Then perlmonks. In that order.
    10. Name an instance where you used a CPAN module. Do I get bonus marks for using my own CPAN module? :-P
    11. How do you open a file for writing? 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.
    12. How would you replace a char in string? $string =~ s/$char/$replacement/g;
    13. How do you store the number of replacements? my $count = ($string =~ s/$char/$replacement/g); Note that the parens are not required, but I think it is clearer with them included.</code>
    14. When would you not use Perl for a project? 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 Cody Pendant (Prior) 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
Re: Interview Prepration
by BUU (Prior) 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?
Re: Interview Prepration
by cog (Parson) on Apr 03, 2005 at 20:07 UTC
    I'll try...

    1. What arguments do you frequently use for the Perl 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)

    2. 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.

    3. 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.

    4. 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...

    5. 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 :-)

    1. 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!

    2. 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...

    3. 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 :-)

    4. 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

    5. 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).

    6. 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 :-\

    7. 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.

    8. 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)

    9. 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.

    10. 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)

    11. How do you open a file for writing?

    Usually with open (FILE, ">$file") or die "could not open file '$file' for writing ($!)";

    12. How would you replace a char in string and how do you

    $string =~ y/a/b/

    13. 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.

    14. When would you not use Perl for a project?

    When Hell freezes over :-P

      "2. 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
      "
      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
        I learned about this from the book "Effective Perl Programming"

        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... :-)

      Round 1, question 3, your answer cannot be reconciled with the syntax @foo{'this', 'that'} = qw(whatever fits);
        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 :-)

        Uh, yes. The words for & foreach are an alias to each other.

      3. 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 :

      use also calls import(). From use:

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

      BEGIN { require Module; import Module LIST; }

      -

      8. 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)

      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 system does) and sometimes, I just open a pipeline.

      "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
Re: Interview Prepration
by talexb (Chancellor) 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.

      1. What arguments do you frequently use for the Perl interpreter and what do they mean?

    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.

      2. What does the command use strict do and why should you use it?

    use strict tells the Perl compiler to be a little pickier about certain syntactical checking. It's almost never a poor choice.

      3. What do the symbols $ @ and % mean when prefixing a variable?

    The dollar sign signifies a scalar, the at sign means an array, and the % means a hash, also known as an associative array.

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

    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.

      5. What are the characteristics of a project that is well suited to Perl?

    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.

      1. Why do you program in Perl?

    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.

      2. Explain the difference between my and local.

    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.

      3. Explain the difference between use and require.

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

      4. Whats your favorite module and why?

    I don't really have favourite module -- the one that handles file names and directories is handy, File Special Functions or something like that.

      5. What is a hash?

    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).

      6. Write a simple (common) regular expression to match an IP address, e-mail address, city-state-zipcode combination.

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

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

    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.

      8. What is the difference between for & foreach, exec & system?

    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.

      9. Where do you go for Perl help?

    I look in perldoc, in the various O'Reilly books that I have, and finally I visit Perl Monks.

      10. Name an instance where you used a CPAN module.

    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.

      11. How do you open a file for writing?

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

      12. How would you replace a char in string and

    I could use s/// or tr/// to replace a character in a string, but it depends on the situation.

      .. how do you 13. store the number of replacements?

    I think tr/// returns the number of replacements, but I'd have to check.

      14. When would you not use Perl for a project?

    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 theorbtwo (Prior) 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. 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 effect printf.) 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
      while (<>)<code> loops, both with and without printing $_.</li> <li>They give the sort of thing returned by the variable expression - +- scalar, list, or hashy list. (Note that the obvious -- they are pa +rt of the name, giving the type of variable -- is slightly wrong in p +erl5, but correct for perl6. The difference is <code>$foo[1]
      vs @foo[1].
    2. Modules and subroutines, including in some cases OO. Also, POD and comments. Also, refs, and in purticular code refs.
    3. 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 complete in another language, and the completed part is not easily wrappable in a library. Another is if the 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.
    1. 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.
    2. my 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.
    3. Use 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.
    4. I'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.
    5. A 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.
    6. Well, none of those problems are purticularly ammenable 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 most email addresses, it will not match all 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.
    7. -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.
    8. There 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.
    9. That 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.
    10. I 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.
    11. Generally, 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".
    12. $string =~ s/a/b/g
    13. my $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.
    14. I 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 CountZero (Bishop) 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

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      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.

Re: Interview Prepration
by cog (Parson) 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 approaches? Will he/she be a nice person to have around? :-)

    All those things count :-)

Re: Interview Prepration
by prostoalex (Scribe) 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?

      Actually, that jobseeker interpretation matches mine; namely, that the OP was looking for the answers in expectation of "being interviewed" 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.

      ww, the monk formerly known as "schodckwm"

        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.

Re: Interview Prepration
by Anonymous Monk on Apr 04, 2005 at 10:57 UTC
    1. 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. 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. 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. All of them. But perhaps "whitespace" is the most important for readability (and hence, reusability). Although, technically, "whitespace" would mean the absence of any element of the Perl language.
    5. 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".
    1. Because I happen to know it, and it often satisfies my needs.
    2. my declares a lexical variable (or a set of lexical variables), local creates a new (or a set of new) value for a non-lexical variable (which is not the same as a package variable).
    3. 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.
    4. It depends on what I want to do.
    5. A mapping that maps strings to arbitrary scalars.
    6. IP 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?
    7. -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.
    8. There's no difference between for and foreach. And system does a fork, execs in the child, waits in the parent.
    9. The manual has all I need.
    10. You see, I wanted to write this program ....
    11. It 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 happely autovivify 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.
    12. _A_ character in a string? As in, a single one? Say, the third character? substr($str, 2, 1, "x") if length($str) >= 3;
    13. Well, 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.
    14. If 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.

        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"; }

        Is this a foreach loop?

        for my $user (@names) { print "Found user $user\n"; }
        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.
        Your opinion 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.
Re: Interview Prepration
by chris7cmcc (Initiate) on Nov 07, 2006 at 18:56 UTC

    RE: local vs. my

    local = dynamic scoping:

    local my 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.

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 they will fit in in your company, instead.