Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Interview Prepration

by Anonymous Monk
on Apr 04, 2005 at 10:57 UTC ( #444621=note: print w/replies, xml ) Need Help??

in reply to Interview Prepration

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

Replies are listed 'Best First'.
Re^2: Interview Prepration
by merlyn (Sage) on Apr 04, 2005 at 17:36 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"; }

      Is this a foreach loop?

      for my $user (@names) { print "Found user $user\n"; }
        Yes, and yes. Spelled "f-o-r", pronounced "foreach". Spelled "f-o-r-e-a-c-h", pronounced "for".

        If you start handwaving "for and foreach are the same" in front of people enough, they wonder why the variable "didn't localize in my for loop... I thought for did that!".

        It really is important to distinguish the two kinds of loops, even though 90% of the time we write "for", we prounounce it "foreach".

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      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.
        That's a bit like saying the difference between George Bush and George W Bush is only the W. After all, you can use George Bush to refer to either one.

        But they really are different people! They're not the same!

        And that's the same with for/foreach. They are not the same! There's a for loop, and there's a foreach loop, and you can use the word "for" or "foreach" to refer to either one, but they are not the same.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      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.
        The question was "What is the difference between for and foreach?" not "What is the difference between the keywords for and foreach?" - it could also mean "What are the differences between the for and foreach loops?" (of which there are several notable differences which perl does not treat the same)

        It's an ambiguous question that should be followed by another question to determine whether it is about the keywords or the loops.

        This post has too many branches :)
        "But what of all those sweet words you spoke in private?"
        "Oh that's just what we call pillow talk, baby, that's all."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://444621]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2021-06-24 09:04 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (124 votes). Check out past polls.