200 likes | 692 Views
Perls of Wisdom. Introduction. PERL - Practical Extraction and Report Language Larry Wall Author of USENET reader rn and patch Extensive training as linguist. English, Reality, and Perl. Reality is a mess. Since English is also a mess, it maps well onto reality.
E N D
Introduction • PERL - Practical Extraction and Report Language • Larry Wall • Author of USENET reader rn and patch • Extensive training as linguist
English, Reality, and Perl • Reality is a mess. • Since English is also a mess, it maps well onto reality. • Perl was designed to be messy (in the nicest possible way.) • Modeled after natural languages; constructs can be complex but meaningful without being wordy (unlike this slide, which is very wordy).
Perl on Simplicity • Perl can be simple, and do many useful things, or it can be complex, and work magic. (Complexity does not necessarily equal code length.)
The Creation of Perl • Originally, made to just be a text processing language. • Larry wanted to fill the void between “manipulexity”and “whipupitude”. • Perl violates a rule of Unix: a tool should do one thing, and do it well.
Perl of Wisdom #1 • There is more than one way to do it.
Don’t have mad skills? • A little bit of Perl can go a long way. A 3 year old can speak English and be understood, despite having a very limited vocabulary. • The same language spoken by a 3 year old is used by philosophers and geniuses to explain extremely complex things.
If all else fails, What Would George Do? • George Would Read The Fine Manual • man perl • If man pages aren’t your thing, perhaps you should invest in a good book. • -w is your friend.
Perl of Wisdom #2 • Easy things should be easy, hard things should be possible • A camel is ugly, stinky, and spits, but gets you where you're going.
Howdy Y’all This is your first coding project in Perl. #!/usr/bin/perl print “Howdy Y’all”; * Note this is an exercise in using the Perl interpreter.
Variable Syntax(or, funny characters you will meet on your journey with Perl…)
Perl Is Type Free $answer = 42; # integer $pi = 3.14159265; # a float $university = “Drexel”; # a string $interp = “I go to $university”; # string + interpolation $noninterp = ‘Its too much $$’; # string – interpolation $output = `pwd`; # output from a shell command $status = system(“vi $x”); # status of a command $dog = new Camel “Fido”; # instantiation of an object
Arrays in Perl @nounsare = (“people”, “places”, “things”); Arrays in Perl generally behave the same way they do in C. $howmanynouns = @nounsare;
Loops • While while (condition) {do this in here; } • For for ($i = 1; $i < 10; $i++) { ... } • Foreach foreach $element (@elements) { do this here }
Filehandles • Allow Perl to interact with real world stuff. • Please close your files. open(SESAME, “filename”) # read from existing open(SESAME, “>filename”) # create file and write to it open(SESAME, “>>filename”) # open existing file and append close(SESAME); # no one likes to see you with # your files open anyway...
Playing With Filehandles A Simple Example: All of these are the same thing. while (<SESAME>) { print $_; } # Don’t panic, I’ll explain the $_ while (<SESAME>) { print; } # Here the $_ is implied print while <SESAME>; Output into files: print SESAME “This goes out to the file.”
Interesting Functions I’ve Met. • print. Not only used as: print “blah”; Try: print << bigblock; # This is more of a print until print this # you hit the label bigblock and this bigblock; • chomp and chop. When taking in strings from files, used to remove last character (usually an annoying newline that can mess things up). chomp is safer, returns # of characters eaten chop returns character eaten • split.
Cartwheels and Backticks • Backticks (` `) can be used to execute commands in a shell. $info = `finger $user` # Returns all output from command as a string # Note interpolation
Your Last Perl of Wisdom • Perl gives the programmer as much rope as he or she desires. • It’s always enough rope to hang yourself with.
The Whole #! • Applied Perl • Perls of Wisdom