What constitutes effective Perl training for non-P

2019-03-08 15:40发布

I've been working with Perl long enough that many of its idiosyncracies have become second nature to me. When new programmers join our group, they frequently have little to no experience with Perl, and it's usually my task to train them (to the extent necessary). I'd like to know what to focus on when training a programmer who is new to Perl but has experience with other languages (this question is meant to be language-agnostic, but most developers I've worked with have come from Java).

A few things occur to me:

  • The proper use of sigils
  • Referencing/Dereferencing
  • Use of list functions like map, grep, sort

Is there anything in particular that you've found it useful to focus on when helping a programmer to transition to Perl? Do you stress the similarities or the differences, or both in equal measure?

19条回答
男人必须洒脱
2楼-- · 2019-03-08 16:05

I'd start by telling them to always use the magical mantra:

use strict;
use warnings;

They don't need to know why (yet).

Next, I'd introduce them to perldoc and show them the basics of using it (the -f and -q flags, perldoc perl and perldoc perltoc, etc.) They need to know where to look when they have questions.

I'd talk about scalar vs. list context.

I'd introduce them to the basic data types (scalars, arrays, and hashes) and how the sigils follow the context, not the data type!

Introduce $_ and @_. It's critical to understand $_ because it's used in so many places, often implicitly. @_ is essential to writing subroutines. The other punctuation variables are used far less often and can wait.

Introduce them to regular expressions and get them used to the idea of regexes being first-class objects in Perl.

I'd briefly show them Perl's "diagonal" syntax. Idioms like next unless /foo/ are wonderful once you get use them but seem very alien when coming from other languages, particularly "orthogonal" ones.

You don't need to talk about references right away. Let that wait until they're ready to do things that require data structures. Functions like map and grep can wait, too.

Build a solid foundation on the fundamentals. Let them take baby steps for a while, then mix in more advanced features and idioms.

查看更多
beautiful°
3楼-- · 2019-03-08 16:07

For C++/Java/C# coders two differences I stress very strongly up-front are that:

No 1: In Perl almost everything has a meaning, just not always what you want. Good examples include:

  • Variable names without sigils don't stop execution - they are interpreted as barewords
  • Try to store an array in a scalar and you get the length
  • Try to store a hash in a scalar and you get 'a/b' (buckets in use vs total buckets?)

No 2: In Perl you can make it up as you go along. In other works you can say "in the hash 'w' the key 'x' indexes an anonymous array where box 'y' holds 'z'" and the interpreter will create and size all the variables for you as you go.

查看更多
唯我独甜
4楼-- · 2019-03-08 16:09

Ok. Sure you need to use strict at some point. And sure you want to use libraries at some point. But strict is just the basic start-the-file-magic for Perl, and each language has libraries. I don't think those are the things which are very important to learn when starting out.

When you start out in Perl, but already know other languages, you need to know crazy idiosyncracies, like $a and $b being special vars, being able to index using -1, etc.

In retrospect, I really like how I learned Perl: By playing Perl Golf.

By using a language to such an extreme, you force yourself to find out all the crazy bits of it, and you really need to understand the different constructs. Especially if you are doing this with a group of friend/colleagues/... this works really well.

(of course this should not be the only training ;) but I really believe it works very well at some point)

查看更多
The star\"
5楼-- · 2019-03-08 16:14

I own and manage Perl Training Australia. I've been teaching Perl for about eight years, and computer science for over a decade. I have strong opinions on not only Perl, but also on teaching, and presenting in general. I've written hundreds of pages of text about Perl -- which I won't be repeating here -- so what I'm going to give you isn't advice on teaching Perl; it's meta-advice instead.

Firstly, if your time and budget allows, consider sending your staff member on a professional Perl training course. Dedicated courses have the advantage that they don't come with work interruptions, they don't come with workplace politics, and they do come with someone who's very familiar with the difficulties people have when learning Perl. Please make sure you have a trainer who knows their stuff and is an active member of the Perl community; it means they should be able to answer any question thrown at them, or direct the questioner to an appropriate reference where they can learn more. Yes, I run a Perl training business, so I'm heavily opinionated here.

If for whatever reason you can't go with a dedicated course, then get a book that's specifically designed to teach people how to program in Perl, and walk through that. It's easy to miss things, or to try and introduce things in the wrong order, or (heaven forbid) teach bad habits, and all of those can make your life difficult. Often the people writing books designed to teach Perl are the same people who have successful Perl training businesses. If you want to buy a book, I'd recommend the latest version of Learning Perl. If you want to download a book, I'd recommend grabbing the Programming Perl course notes from the Perl Training Australia website.

Both of these books come with exercises, and this brings me to my last piece of meta-advice. Make sure anyone who is learning Perl does the exercises. It's very easy when learning any new skill to think you know what's going on, but discover that when putting things into practice it's harder than it looks. This is particularly the case with Perl, where programming concepts like "context" can apply, which are rare in other languages. Usually the exercises are specifically designed to teach a certain skill, or to highlight a certain pitfall; figuring these things out during learning is much easier than figuring them out on the eve of a project deadline.

查看更多
再贱就再见
6楼-- · 2019-03-08 16:14

I'm a java programmer who recently had to pick up Perl. The part I found hardest to get used to were all the special built in variables like $_ $~ $' and so on. Until you get used to them it's hard to keep track of which one does what.

And, of course, the use of regular expressions.

For example, I have to maintain code and when I saw the line below for the first time it was a little confusing. As a java programmer it looks like gibberish.

next unless "$_" !~ /^#/;
查看更多
迷人小祖宗
7楼-- · 2019-03-08 16:16

Programming Perl (coauthored by #22483 - good work Randal, i've had mine for near on 10 years) was a great intro to Perl. Starting with regex, slicing and dicing arrays, associative arrays etc. Plus the whys and hows of reporting in Perl and a function reference.

I'm not sure how much it's changed in it's third edition but i would base a course on it and use it as the text book. It's reasonably terse and to the point. Plus then your course matches your documentation.

查看更多
登录 后发表回答