Saturday, March 10, 2007

PickupPerl & Screen

I read through PickupPerl in a few days. It's a simple but good starting point. As I am familiar with C programming, I only list what is important to me. As an exercise to study, I started to write a script for my data backup. Meanwhile I found some other good tutorials with more details @www.perl.com, an official perl website.

I also found a pretty handy tool, screen, a terminal manager. Just remember a few often used commands. There is a good introduction @ http://jmcpherson.org/screen.html

Perl

1. The first example. Just remember it.

#!/usr/bin/perl

use strict;
use warnings;

print "What is your username? ";
my $username;
$username = ;
chomp($username);
print "Hello, $username.\n";

2. Scalar variable can be either a string or a number. And a scalar variable is not defined as a string or a number. Perl does automatic conversion between string and number.

3. Single-quoted string. no special character except ' and \. Note \' and \\ is replaced but backslash followed by something else keeps both. For instance \o means \o, two characters.

4. New line can be embedded into a single-quoted string.

5. Double-quoted string is similar to that in C except that $ and @ are special characters and need backslash.

6. For number, you can use underscore for readability. It's just for readability.

7. Scalar variable support interpolation.

8. Undefined variable is allowed and assigned a special value 'undef'.

9. 0, undef, "" means false and others means true.

10. <=> cmp, . string concatenation, x string duplication.

11. Names are case sensitive.

12. Array(@) can grow and shrink dynamically as needed by setting $#array. When an array is growing very large very quickly, it can be a bit inefficient. You can pre-build an array of certain size instead. $#array: subscript of the last element in the array.

13. Everything in the array must be a scalar. No 2-D array. Array in another array simply concatenate each other.

14. Array quoted by () or qw//. Use .. with slice.

16. Push, pop at the end of array, shift/enshift at the beginning of array.

17. blocks are quoted by {}. Variables declared in the block is valid only in the block.

18. if/elsif/else, foreach. foreach my $item (@collection) { print "$item\n"; }

20. hashes(%) are not subscripted by numbers but an arbitrary scalar value. A hash is a list of value pairs, key and value. keys/values %hash return array of key or value.

22. Regular expression(quoted by //). * zero or more of the previous character; . any; | or ; () grouping; ^ start of string; $ end of string; =~ for pattern matching. \d match [0-9]. check 'perlre' for more.

24. Subrouting. sub subname { xxx; }, return; parameter. special array variable @_.


Screen

C-A c create
C-A p/n page forward/backward
C-A 0/1/2.. select a window
C-A d/r detach/retach

1 comment:

QuickWayne said...

Some good Perl website

1. http://johnbokma.com/perl/
2. man perl
3. www.perl.org
4. www.perl.com