|
|
Intro to Perl - Day 1
Admin
Roll, parking passes.
Goals
In this session you will:
- see some important perl resources
- learn the basic nature of perl
- learn when to use perl, and when not to
- first simple program
- discuss main data types
- input from STDIN
- work with regular expressions
Perl Resources
(See resources page)
Perl
- uberscripting language combining elements of sed, awk, grep, shell scripts, C, etc
- pseudo-compiled (compiled at run-time)
- many functions (v. 20-something)
- freeform, allows great flexibility in arrangement
- pros
- fast development, no compile step
- forgiving and not picky
- free!
- portable
- outstanding text-handling abilities
- cons
- big (requires interpreter)
- slow
- exposed sourcecode
- easy to learn, hard to master
Hello, World
Prepare yourself for debugging! It's OK! You're OK! :-)
- pick a text editor: vi, pico, joe, jove, emacs, whatever
 Trick of the Trade |
Pick a text editor, and learn to love it. Your editor is your best friend when programming in script languages. Familiarity with your text editor is probably the most important productivity tool you can develop at this point. |
- exercise: open a file
hello.pl
- line 1: magicline, magic cookie
#!/path/to/perl -w
- similar to other shells
- warn flag to help debug
- line 2:
print ("Hello, world!\n");
- function
- defaults to STDOUT
- parens delimit the arg passed to the function. Optional but visually helpful
- text delimited by quotes
- escape code for newline
- all statements end with a ";"
- make it run by referring to it on the command line (ID common errors)
- troubleshoot common errors
- relax!
Variables
- no declarations!
- $scalars
- nums or chars, automagically sized
 Remember this! |
perl is not (as far as we are concerned) strongly typed. This means that scalar variables are not designated as float char int, etc. A side effect is that perl will make an educated about whether you want to use a variable as text, number, etc.
You will see many situations where a variable has different values when it is evaluated in different contexts. |
- @arrays and $array[elements]
- %hashes and $hash{keys}
- no order! seemingly random!
Working with Scalars
- exercise: make a new script
vars.pl to practice initializing variables.
- exercise: STDIN
- $var=<STDIN>
- exercise: build simple script that asks for your first name, then prints out
hello, firstname
- linefeeds, null chars, chomp and chop.
- 'plain', "interpolated vars, control chars (34)", `executed in a shell`
- +, -, /, *, ++, --, +=, -=, *= exercise modify the script to add, and to increment.
- concatenation - exercise modify the script to concatenate two strings. What happens if you concatenate numbers?.
- comparators (37)
- nums or chars, automagically sized
Regular expressions
A regular expression is a pattern or model that describes an occurrence of characters (bytes)
- matching:
m/regex/
- substitution:
s/regex/replacement/g
- translation
- forcing case:
$name = "\U$name";
- translation operator:
tr/abc/123/
Homework
For the next class you will need to look at this material:
- chomp(), chop() 43-44
- Array elements, 52-55
- STDIN as array, 56
- control structures, 58-65
- hashes 66-71
FYI, also read:
- Precedence and Associativity, 38-39.
http://www.mousetrap.net/introperl/day1.html
$Id: day1.orb,v 1.18 2002/10/28 18:27:01 mouse Exp $
|
© 1994-2002 jason carr.
distributed under the terms of the GNU Free Documentation License.
jason carr
Reminders
- Classroom temperature can be wildly variable. Dress lightly and bring layers.
- your username is based on the class title and the last two digits of your workstation's hostname.
- remember to take your work with you.
|
|