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

RFC: Self Assessment Perl

by LanX (Bishop)
on Sep 05, 2018 at 18:38 UTC ( #1221788=perlmeditation: print w/replies, xml ) Need Help??

Some weeks ago I was tasked to give two new colleagues a quick 3 days Perl training/fresh-up.

Suddenly many different people with different (self-proclaimed) skills wanted to join.

So I started to brainstorm about traps and misunderstandings I encountered in 10 years of monastery ...

Then I mailed this list of points taken from perlglossary , each with at least one exemplary question.

I thought it might be beneficiary to share it here...

NB: there is no order / prioritization / completeness.

But this list of different subjects led to interesting insights of the coding habits of my colleagues...

Self Assessment

So, do you know Perl?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re: RFC: Self Assessment Perl
by Your Mother (Bishop) on Sep 05, 2018 at 19:09 UTC

    That’s a wonderful list that neatly condenses down many things that took me a couple of years to learn. One gotcha I might suggest, probably as an adjunct to something existing or perhaps as a part of “Passing arguments.”

    moo@cow[49]~>perl -le 'print ( 1 + 2 ) / 2' 3

    Oh, and command line switches would be extremely valuable. Say what you want about Perl v Python v Ruby v Go v Scala, et cetera, but Perl on the command line is a game changer for anyone willing to take it up.

      > That’s a wonderful list that neatly condenses down many things that took me a couple of years to learn.

      Thanks, I somehow wanted to nail down mental coordinates of the "Perl hyper space". But just dropping words wouldn't help because most people would just assume something and nod.

      But asking a question puts them under stress and motivates them to rethink and gives an incentive to do some research.

      And at least I can point to that list next time people "try to misunderstand" me in meetings... ;-D

      > print ( 1 + 2 ) / 2

      I could add this to "precedence"

      > command line switches

      Unfortunately it's a very stubborn windows context here at $job$, i.e. really working with a shell/cmd is out of scope for most of our team ...

      I know the list is incomplete ...

      ( actually I was sure I also mentioned autovivification, but somehow this extended list got lost ) ...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: RFC: Self Assessment Perl -- special variables
by Discipulus (Monsignor) on Sep 05, 2018 at 21:02 UTC
      $h = {%h} personally I never seen used this way and I'd stress the standard form to take reference as in \%h
      They do different things; as the {%h} will get you a reference to a shallow copy of the hash, not just a reference to the original.
      >perl -e "my %h = (1,2); my $g = {%h}; $g->{1}=3; print $h{1} . ' vs ' + . $g->{1};" 2 vs 3
      I see you've spotted that in the update tho.
      > Special variables

      Almost all special variables are very edge case. They can look them up, provided they understand perldoc.

      I was concentrating on essential concepts which are misunderstood or ignored, like "flattening".

      This is not meant to be a tutorial *, people should rather realize where the gaps in their knowledge are.

      You can't explain things to people who don't really listen because they think they know it already.

      > regex ... map/grep/sort

      my colleagues are capable to write millions of LOC without any regex° or map/grep/sort

      This should probably be on a secondary list, I didn't want to risk that they depreciate the fist list. (hope you know what I mean)

      > $h = {%h} personally I never seen used this way and I'd stress the standard form to take reference as in \%h

      Seems like someone misunderstood flattening.... ;-P

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      *) though it could link into a tutorial or training.

      °) well they sometimes use "cargo cult" regexes, i.e. C&P without understanding.

      update

      That's very a good idea

      • Core modules
        Name some standard modules/pragmas you use and explain what for.

      they are confronted with CORE, the concept of module installation and can interact with each other.

Re: RFC: Self Assessment Perl
by Eily (Prior) on Sep 06, 2018 at 13:40 UTC

    I also think this list is really nice, with its wide variety of aspects of perl tricks and specificities :). Indicating how critical each piece of knowledge is might be useful though? Mastering context isn't required but knowing the basics can help a lot. You can get quite good with perl without having to understand BEGIN or even the notion of compile/run time, so I was kind of surprised that this was the first question.

    Where can I look this up?
    Best question. Knowing what kind of information you can get out of the doc, like perlop (like operator precedence, but also all the stuff on the quote like operators), perlvar (like knowing that sometimes you change the behaviour of a function by changing a var, rather than with the parameters) should be high on the list of required knowledge.

    Booleans:
    There are more than four? Actually this made me realize that I didn't know the answer to that correctly, I thought that any string that "looks like a number" and is equal to 0 was false. But:
    perl -E "say for grep { $_ } qw<0 00 0.0>" 00 0.0
    This means that the four FALSE I can think of are "", 0, "0" and undef. I suppose the empty list might count? (Edit: actually there are two numbers that are false, int 0 and real 0.0, so I guess that's up to 6 false values?)
    Also I'm not sure about the other question, is this about how perl tests truth internally, or are you expecting an answer like !!(FalseOrWrong)?

      > Indicating how critical each piece of knowledge is might be useful though?

      as I said I didn't prioritize, and things are too interconnected.

      like {%h} is not only a ref to an anonymous hash but the %h is flattened inside the { LIST } operation.

      > You can get quite good with perl without having to understand BEGIN or even the notion of compile/run time, so I was kind of surprised that this was the first question.

      My colleagues have to understand modules, for that to understand they need compile-time and namespaces

      (I'm sick of all the requires into main:: in our code...)

      Variable declaration happens at compile-time, die'ing at run-time should be avoided , ... and so on.

      > Where can I look this up? ... Best question.

      yes but they also need a mental map of essential key words.

      A condensed Perlglossary would be nice, probably more explicit and with less Larry injokes.

      > Actually this made me realize that I didn't know the answer to that correctly

      Mission accomplished! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        I think you don't need to understand BEGIN to understand modules at a certain level. I think a useful lie is that use is just like require+->import(), and to postpone the discussion of BEGIN.

        You can always gloss over that by saying that use must appear at the top of any code.

        Maybe you could add "what are the TWO differences between use and require?" to your list of questions then.

Re: RFC: Self Assessment Perl
by davido (Cardinal) on Sep 06, 2018 at 17:03 UTC

    Definedness: Explain the difference between false and undefined. Show how to test for each.

    Hashes: Show how to test for the existence of a hash key.

    Autovivification: Show how to fetch the value in $foo->{'bar'}->{'baz'} without causing $foo->{'bar'} to spring into existence if it didn't exist to begin with.

    Sorting: Show how to sort "foo123", "bar123", "baz345", "baz123" numerically first, alphabetically second.

    List manipulation: Show how to find how many times the number 5 exists in map {int(rand(6)+1)} 1..20.


    Dave

      Nice :)

      Interview questions?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: RFC: Self Assessment Perl
by zentara (Archbishop) on Sep 06, 2018 at 11:52 UTC
    This would also be a great Perl Job Interview question set.

    As far as self-assesment goes, I would fail unless it was an open-book or with-laptop test. :-)


    I'm not really a human, but I play one on earth. ..... an animated JAPH
      > This would also be a great Perl Job Interview question set.

      Unfortunately, I don't want people to learn answers by heart.

      I once had an Skype interview with "teleprompter" interviewers who had no idea what they where reading and what to expect ...

      > I would fail unless it was an open-book or with-laptop test. :-)

      The idea is to give orientation and an incentive to open a book...

      update

      But from a certain perspective you are right. I'm somehow interviewing my colleagues ...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Yes teacher..........I see your point completely. :-) How's that for politeness?

        I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: RFC: Self Assessment Perl
by eyepopslikeamosquito (Chancellor) on Sep 07, 2018 at 07:33 UTC

    See On Interviewing and Interview Questions for how I used to go about interviewing.

    Further to that, I've dug out some old Perl interview questions I used to assess candidates who said they "knew" Perl (to keep them honest). That is, these questions are not difficult for a Perl expert.

    • What is the CPAN?
    • What does use strict; mean?
    • What is the difference between $x eq $y and $x == $y?
    • What is the definition of "false" in Perl?
    • Generally, when should you use 'foreach' versus 'map' versus 'grep'?
    • What is the difference between use File::Find and use File::Find () and require File::Find?
    • What is the difference between 'my', 'local' and 'our'?
    • In a regex, how do you do non-capturing parens?
    • In a regex, what's the difference between "greedy" and "non greedy"?
    • What is a regex assertion? Which ones have you used?

    Given a list of numbers, namely:

    my @oldlist = ( 4, 7, 8 );
    write some code to add 42 to every item in this list, producing a new list. For this example data, newlist should contain the values: ( 46, 49, 50 ). Sample answer:

    my @oldlist = ( 4, 7, 8 ); my @newlist = map($_ + 42, @oldlist);

    Given a string containing a space-separated list of names:

    my $names = "freddy fred bill jock kevin andrew kevin kevin jock";
    write some code to produce a frequency table of names, sorted descending by frequency, then ascending by name. For this data, the output should be:
    kevin : 3 jock : 2 andrew : 1 bill : 1 fred : 1 freddy : 1

    Sample answer:

    my $names = "freddy fred bill jock kevin andrew kevin kevin jock"; my %freq; for my $name (split ' ', $names) { ++$freq{$name}; } for my $k (sort { $freq{$b} <=> $freq{$a} || $a cmp $b } keys %fre +q) { printf "%-10s: %d\n", $k, $freq{$k}; }

    Given an input text file and an output file as follows:

    my $infile = 'in.tmp'; my $outfile = 'out.tmp';
    write some code to read infile and change all occurrences of 'Peking' to 'Beijing', leaving infile unchanged and writing the changed text to a new file outfile. Sample answer:

    my $infile = 'in.tmp'; my $outfile = 'out.tmp'; open(my $fhin, '<', $infile) or die "error: open '$infile': $!"; open(my $fhout, '>', $outfile) or die "error: open '$outfile': $!" +; while (my $line = <$fhin>) { $line =~ s/\bPeking\b/Beijing/g; print $fhout $line; } close($fhin); close($fhout);
    If they use s/Peking/Beijing/g contrast with s/\bPeking\b/Beijing/g and ask which they prefer.

    Updated: Added \b assertions to s/Peking/Beijing/ and extra questions around regex assertions. Thanks haukex.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1221788]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-09-26 01:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (205 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!