1 / 20

Perls of Wisdom

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.

jaden
Télécharger la présentation

Perls of Wisdom

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perls of Wisdom

  2. Introduction • PERL - Practical Extraction and Report Language • Larry Wall • Author of USENET reader rn and patch • Extensive training as linguist

  3. 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).

  4. 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.)

  5. 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.

  6. Perl of Wisdom #1 • There is more than one way to do it.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. Variable Syntax(or, funny characters you will meet on your journey with Perl…)

  12. 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

  13. Arrays in Perl @nounsare = (“people”, “places”, “things”); Arrays in Perl generally behave the same way they do in C. $howmanynouns = @nounsare;

  14. Loops • While while (condition) {do this in here; } • For for ($i = 1; $i < 10; $i++) { ... } • Foreach foreach $element (@elements) { do this here }

  15. 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...

  16. 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.”

  17. 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.

  18. 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

  19. 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.

  20. The Whole #! • Applied Perl • Perls of Wisdom

More Related