What should I teach a beginning Perl programmer?

I am going to spend 30 minutes teaching Perl to an experienced programmer. The best way to learn Perl is by writing code. In addition to CPAN, what would you show a programmer so they would understand the expressiveness of Perl, the amount of functionality provided by CPAN, while keeping everything clean and tidy so they walk away comfortable with the language? I'll save the tricky stuff for another day.

use warnings;
use strict;
# use A_CPAN_LIB;

sub example_func1 {
  # use the CPAN lib or demonstrate some basic feature of Perl
}
example_func1();
# ...
__END__


Here's what I came up with...

Where to Start

Believe it or not, the man pages. Ok, we'll just use perldoc instead to be Windows friendly.

The perldoc pages (or man pages on Unix/Mac) are excellent for Perl. You can type man perl or perldoc perl

perldoc perl; # Show an overview and dozens of tutorials; man perl is the same.

perldoc perlintro; # A Perl intro for beginners; man perlintro perldoc perlrequick; # An example Perl regex tutoral

perldoc perlfunc; # Shows builtin Perl functions perldoc perlre; # More Perl regex.

CPAN

There are thousands of libraries on the Perl library site CPAN. perl -MCPAN -e 'install DateTime'

perldoc works for installed modules too: perldoc module

perldoc DateTime perldoc DBI; # Database API. If this doesn't work then install it:          perl -MCPAN -e 'install DBI'

Recommended Modules

perl -MCPAN -e 'install Moose'; # Perl does OOP perldoc Moose; # Tell me more about the Moose perl -MCPAN -e 'install CGI'; # Quick and dirty web pages perl -MCPAN -e 'install Catalyst'; # Big web framework. Sometimes have problems installing. Google is your friend perl -MCPAN -e 'install CGI::Application'; # Another web framework perldoc CGI::Application; # Take a quick look at the docs A little Q&A. Q: Why should I use Perl instead Ruby or Python? A: More people use Perl. There are more libraries for Perl(way more). Perl is a really great GTD language. Q: Why do people hate Perl? A: You can do some ugly stuff with it. Remember use warnings; use strict; in all of your code. You can check your code before running it. perl -c hello.pl

Perl Topics
Using Perl with Databases

http://www.perl.com/pub/a/1999/10/DBI.html

Using Perl for Web Development

http://www.catalystframework.org

OO Perl

http://www.iinteractive.com/moose

Perl 1-Liners

http://www.perlmonks.org/?node_id=470397 http://sial.org/howto/perl/one-liner

Other Tutorials

http://perlmonks.org/index.pl?node=Tutorials

Books

There are dozens. http://www.amazon.com/s/ref=nb_ss_gw?url=search-alias%3Dstripbooks&field-keywords=perl&x=0&y=0

Websites

Perlmonks Perl.org Pleac StackOverFlow's Hidden Features of Perl CPAN FAQ Randall Schwartz's articles

Getting Help

Perl Nabble Forum IRC Channels: freenode, irc.perl.org. There are several: irc://irc.perl.org/perl irc://irc.perl.org/catalyst irc://irc.freenode.net/modperl irc://irc.perl.org/perl6

Answers


We wrote a whole book on that called Learning Perl. Check out the table of contents.

This book is a product of teaching people Perl since 1995. It's not geared to any particular application, and shows people the parts of Perl that they'll use for 80% of their Perl programming. We updated it for Perl 5.10, and include sections on using CPAN.

Good luck, :)


Perl has two things that are likely to be foreign even to experienced programmers, so those should perhaps be mentioned early on so they don't run away screaming.

Context: Nearly every function in Perl has 2 behaviors. When called in a "scalar context" and it does one thing, when called in a "list context" it does something else instead. This may seem weird and strange (and it is, in machine languages) but is simply the natural language concept of "singular" and "plural" applied to a programming language.

Variables: Perl has 2 completely different and separate systems of variables. Lexical variables (my) and package variables (our). Lexical variables are "normal" if you've used most any programming language. Package variables (i.e. dynamic variables) are strange, unless you've used something like Lisp. "Always prefer lexical variables over package variables, except when you can't."


Definitely show them how easy it is to use regular expressions in Perl.


That's basically a task-oriented question.

If they are to use it for parsing, show them how easy manipulating STDIN and file i/o is.

If they are going to use it for databases, show them how to get hashrefs from query results and that should wow them.

Perl usually has some way to make just about any task super-quick. Pick out the task they need to do.

But definitely teach them to use my and local. Stress the importance of my and that will make their experiences happier.


Experienced programmer or not, in 30 minutes you can't pretend to teach anything, let alone Perl. At most you can try to enlighten him with some cool one-liners (provide their full-bloated Java counterpart, for comparison).


I'd cover lists and hashes first. (Pathologically Eclectlic Rubbish Lister, remember.) Show him how much prettier foreach is than a C-style for.

If he's coming from C/C++ it would be good to refer him to http://perldoc.perl.org/perltrap.html or 'perldoc perltrap.' It contains the most obvious differences to be aware of.


Just my 2c, but in relation to CPAN, how about you pose them the problem of splitting English text into sentences?

At first, that seems simple: a sentence is a string with a period at the end.

But after a moment's thought, a programmer will find there are all kinds of complexities which arise. Periods can be in the middle, if there are decimal numbers or abbreviations; sentences can end with other things, like "?", "!" or "..."; 'a period followed by a space' doesn't help either because what about EOF?

Long story short, when it comes to Perl, someone else has thought of everything on that list and more. So you use Lingua::EN::Sentence.


If he’s an experienced programmer, he might like Smart Comments, POD, closures, the -d:DProf switch and dprofpp, one-liners, Perl Critic, Moose, __DATA__ or map. (Which is a crazy mix indeed.) I’d explain to him right from the start that Perl is a language with a lot of magic, but that he is free to choose when to stick to simple code and when to draw a wand. Experienced programmers are not afraid of choices :-)


The idea that popped into my head, was to have them transfer information from one format to another. For example, getting xml data, and transfer it to JSON, for use on a web-page.

cpan JSON XML::Simple
use strict;
use warnings;

use JSON;
use XML::Simple;

my $data;
{
  open( my $file, '<', 'filename.xml' ) or die;
  $data = XMLin($file);
  close $file;
}
{
  open( my $file, '>', 'filename.json' ) or die;
  print $file to_json( $data );
  close $file;
}

I agree with some of the other commenters that it truly depends on the type of task that Perl is being used for. Is this person a Windows system admin? Then I'd cover using WMI from Perl (scriptomatic would be a good resource here).

I'd grab a copy of Oreilley's "Perl Cookbook", and find some interesting topics out of there. Here is a link to the book here: link text

One of my favorite things in Perl is how easy it is to compare lists, looking for unions, intersections, or differences in unique lists (recipe 4.9 in Perl Cookbook). Helps you appreciate the power of Perl.


It depends on what kind of programming this 'Experienced Programmer' is experienced in.

If they've done much shell programming, they'll probably be impressed by Perl in its super-awk personality - do some practical extraction and reporting by using regexes and templates.

If they're more like C programmers who like to work with complex data structures, show them how easily you can whip up a hash of hashes, and how quickly the resulting code executes.

... and so on.


Need Your Help

Login/Logout links in database driven navigation?

php zend-framework zend-navigation zend-acl

I have a database driven navigation mainly composed of two tables: menus and a menu_items, this works out fine for purely "static" links but now I need to have a dynamic link ( login/logout ).