Slideshow transcript
Slide 1: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Perl: the P is for Practical a very short course by Randal L. Schwartz Stonehenge Consulting Services, Inc. Version 1.1.0 (4/26/00)[SLE] Copyright ©2000 by Randal L. Schwartz, Stonehenge Consulting Services, Inc. Page 1 of 21
Slide 2: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM What this course is about • Brief introduction to Perl • Implications of using Perl in a practical business environment • References for further information Page 2 of 21
Slide 3: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Larry Wall • Larry Wall studied linguistics, but eventually went into software • He wrote rn and patch, and gave them away • When he needed something better than awk, he wrote a language for his own use • He called it Perl—Practical Extraction and Report Language • Larry retains rights to Perl (all work performed on his own time) Page 3 of 21
Slide 4: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM History of Perl • That first release of Perl was in 1987: Ronald Reagan was President, no one had yet heard of the movie Who Framed Roger Rabbit, and the Soviet Union was experiencing glasnost Version Release date Documentation Important Perl news 0 1987 Larry Larry and his officemates used Perl 1 1987 10 pages Public release of Perl 2 1988 20 pages Henry Spencer’s Regular Expression package 3 1989-91 60 pages Ability to work with binary data 4 1991-93 75 pages First edition of Camel book published 5 1994-present over 1000 pages Complete re-write of the source, modules, lexical scoping, tied variables, user-extensibility 5.6 2000 around 1200 pages Lexical warnings 6? ? ? Topaz? Page 4 of 21
Slide 5: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Modern Perl • Larry promises that Perl will always be free! • These days, most development is being done by p5p (the “Perl 5 Porters”) • p5p consists of around 30 key people and hundreds of others from all over the world • Nearly everyone works on Perl as a labor of love: Perl helps us, and we help Perl • p5p rule number one: Larry is always right • p5p rule number two: After Larry changes his mind, he’s still right • Overriding design philosophy: “Common things should be easy; advanced things should at least be possible” • Larry’s currently working for O’Reilly (job description: “just be Larry”) Page 5 of 21
Slide 6: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Purpose of Perl • Data slogging, process management, generic tool box for Unix (“duct-tape of the Internet”1) • Fills the gap between C programming (usually hard and ugly, but fast and unlimited) and shell programming (slow, hard, ugly, and limited)—Perl is easy, kind of ugly, mostly fast, and nearly unlimited • Very High Level Language (code is more dense, therefore faster to code, shorter to debug) • Much more portable than shell scripts, Java, INTERCAL, Ada, or C • Not for writing distributed proprietary code (no opaque binaries) • Good for quick and dirty, or long and extensive hacking • Why another language? Perl is optimized for problems which are about 90% text and 10% something else; that description also fits most programming tasks2 1. Hassan Schroeder of Sun Microsystems, used to be quoted in http://www.webdeveloper.com/spring96/duke.html 2. “If you want to program in C, program in C. It’s a nice language. I use it occasionally.…”—Larry Wall Page 6 of 21
Slide 7: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Availability • Everywhere (like Elvis Starbucks) • Released under the GNU Copyleft, or Larry’s Artistic License, whichever you like • Source code, and lots more, at any Comprehensive Perl Archive Network (CPAN) site • Nearest CPAN site can be located via http://www.perl.com/CPAN/ • Also on the GNU tape CD-ROM • Also included with many vendors’ systems • Distribution is self-configuring for nearly anything that calls itself Unix and has a C compiler • Non-UNIX binaries available via CPAN—Acorn, Amiga, AOS, AS400, Atari, BeOS, LynxOS, Mac, MPE, MS-DOS, Netware, MVS, OS/2, Plan9, QNX ,VOS, VMS, Windows 95/98/NT/2000 Page 7 of 21
Slide 8: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Support • Well, you get complete source—You get to fix the bugs yourself! • No source code escrow—thousands of people all help to maintain Perl • If you actually find a real bug in Perl, use the perlbug utility to report it to p5p • It’s not unusual to get an answer from p5p in minutes • Find your local Perl Mongers (http://www.pm.org/) • The full documentation comes with Perl—use the perldoc command to read it, or your system’s native documentation system • Volunteers on the Internet—post questions to comp.lang.perl.moderated or comp.lang.perl.misc • Or write us (addresses on page 21)— we’ll answer short questions for free! • Commercial support (http://www.perlsupport.com/) Page 8 of 21
Slide 9: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Perl books • Programming Perl, Second Edition (a Nutshell handbook from O’Reilly and Associates) by Larry Wall, Tom Christiansen, and Randal L. Schwartz, ISBN 1-56592-149-6 (Camel book) • Learning Perl, Second Edition (another Nutshell book) by Randal L. Schwartz and Tom Christiansen (foreword by Larry Wall), ISBN 1-56592-284-0 (Llama book) • Learning Perl on Win32 Systems (another Nutshell book) ISBN 1-56592-324-3 (Gecko book) • Effective Perl Programming (from Addison-Weseley) by Joseph Hall and Randal L. Schwartz, ISBN 0-201-41975-0 • Perl 5 Pocket Reference by Johan Vromans, ISBN 1-56592-495-9 • Other books available (new ones nearly every month!) • For more recommended books, see http://www.stonehenge.com/books/ Page 9 of 21
Slide 10: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Making the program • Program is a text file, edited with ordinary text editor • Any text editor may be used, but a programmers’ text editor, like vi or emacs, is recommended • Say you’ve typed in the “Hello, world!” program #!/usr/bin/perl print \"Hello, world!\\n\"; • Save it under any name you wish; no extension is generally needed (or recommended) • A non-Unix system may require an extension like .plx; see the notes for your distribution • The program is then made executable like a shell script (on Unix systems) $ chmod +x my_program the $ represents the shell prompt • Now you can try running it $ ./my_program the ./ means “this program is found in the current directory” • Bugs? Edit, run, and repeat as needed (but you don’t need to chmod each time) Page 10 of 21
Slide 11: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Whitespace and comments • Whitespace (spaces, tabs, and newlines) is generally insignificant • Most programs use pretty standard use of whitespace, like most of the code shown here • Comments run from the pound-sign (“#”) to end of line print # This is a comment \"Hello, world!\\n\" a functional (but weird) “line” of code ; • The #!-line is a special comment: If the very first two characters on the very first line are #!, then what follows is the program which executes that file, on Unix • On most non-Unix systems, it’s traditional (and even useful) to use #!perl, since the path doesn’t generally matter • You may need to use /usr/local/bin/perl or something else instead; ask your local expert • If the #!-line is wrong, you’ll probably get an error from your shell (perhaps permission denied or file not found) Page 11 of 21
Slide 12: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM General concepts • The “main” program consists of all statements not within subroutines • There is no required “variable declaration section”, as in some other languages • Syntax is C-like, with everything else thrown in (the best of many worlds) • Some sections of code are typically indented to help the humans who read the code—but the computers don’t care about whitespace • Most statements are an expression followed by semicolon print \"Hello, world!\\n\"; the statement we’ve already seen Page 12 of 21
Slide 13: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM More example programs • This program translates and displays the output of an external program #!/usr/bin/perl @lines = `perldoc -u -f atan2`; run an external program foreach (@lines) { s/\\w<([^>]+)>/\\U$1/g; process the output print; } • Count the words in a document while (<>) { for $word (split) { $count{$word}++; } } for (sort keys %count) { print \"$_ was seen $count{$word} times\\n\" } Page 13 of 21
Slide 14: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Web-related samples • Get my home page use LWP::Simple; $content = get \"http://www.stonehenge.com/merlyn/\"; • Mail me the results from a web form submission use CGI qw(:all); use Mail::Sendmail; my $name = param('name'); my $age = param('age'); sendmail( To => 'perl-training-info@stonehenge.com', Message => \"name is $name and age is $age\", ); print header, start_html(\"Thanks\"), h1(\"Thanks\"), p(\"Thanks!\"), end_html; • Many actual production programs are indeed this short! Page 14 of 21
Slide 15: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Who uses Perl? • Large user community • Best estimates from two years ago are 2 million users, with 200 new users a day • Darling language of the web—some estimates say 50% of all form submission is handled with Perl • Has been embedded or bundled with commercial applications • Casual use by the occasional “scripting” of a System Administrator (the original use) • Intense use by the new legions of full-time “Perl Programmers” (RAD, Web Whacking) Page 15 of 21
Slide 16: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Who uses Perl, really? • Stonehenge client list includes: ADP, Allen Bradley (Rockwell Automation), Allied-Signal, American Online, Arco (Anchorage), Boeing (and Boeing Computing Services), Cisco Systems, Cray Research, DigitalWork, Dow Chemical, Dow Jones, Dynamic Web Enterprises, etoys.com, Genentech, Hewlett-Packard (Corvallis, Cupertino), Intel (Hillsboro, Folsom), Intelsat, Interactive Systems, Lehman Bros., MIPS, Matthew-Bender, Mentor Graphics (UK), Morgan Stanley, Motorola (Austin, Chicago, Phoenix, Fort Worth), NASA/Ames, NASA/JPL (the first real home of Perl :-), Network Equipment Technologies, Nokia, Octel, Pyramid, Qualcomm, Rational Software Corp, SCT Corporation, Shell Systems International, Silicon Graphics, Silicon Systems (Orange County, Santa Cruz), State Farm Insurance, Texaco, Union Bank of Switzerland, United States Army, United States Census Bureau, University of Buffalo (staff), Washington Post \"dot Com\" (Digital Ink),World Bank • Other notables: Yahoo, Altavista, Deja, Filepile, Internet Movie Database, etc etc • It’s probably easier to say who isn’t using Perl Page 16 of 21
Slide 17: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Perl as Glue • Perl is friendly to other programs and connections • Perl can launch child processes with full control (pipe, fork, waitpid, file-descriptor shuffling) • Full TCP/IP sockets • You can write a complete web server in Perl in a few lines of code • Ditto web clients, mail clients, NNTP clients, Telnet clients, arbitrary socket interconnections • Full System V IPC (shared memory, semaphores) • Embed Perl in C or C++ • Attach C or C++ to Perl • Intertwine Perl and Java (JPL) (not for client-side apps) • Taint checking permits some degree of safety in apps that act on behalf of others Page 17 of 21
Slide 18: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Optimized for “90% text, 10% something else” • Many problems involve shuffling text around • Text comes from user input • Text can also come from other programs • Important to recognize patterns and extract useful information • Perl’s text processing capabilities are excellent • No “upper limit” on size of anything to declare or worry about (no “stack-smashing” security holes) • But Perl also has the other stuff • Like binary data, linkage to C, sockets, database connections • These are often parts of the same problem, and Perl can do it • Unlike most other languages, all the memory management (size and reuse) is automatic Page 18 of 21
Slide 19: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Perl supports “programming in the large” • Perl has a built-in warning system to note questionable code • Separate modes for small vs. large programs (can require programmers to declare variables) • Packages make possible sharing of top-level namespace (everyone can have their own private $idaho if they want) • Full support for Object-Oriented Programming available, but optional (most small programs don’t use any OO at all) • Extensive CPAN support gives most common tasks a rapid solution • Interactive symbolic debugger built in • Good runtime profiling and measuring tools available from CPAN • GUI IDEs available from third-party commercial solutions Page 19 of 21
Slide 20: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Good books and courses available • Good quality documentation is available for free • Documentation provided with Perl distribution • Many more things available from the net (start at http://www.perl.com/) • Or, nominal cost commercially produced books and magazines cover Perl as well • Good Perl training is available worldwide from companies like Stonehenge (http://www.stonehenge.com/perltraining/) Page 20 of 21
Slide 21: STONEHENGE CONSULTING SERVICES,INC 2828 SW Naito Parkway, Suite 300 Portland OR 97201(503)777-0095 TM Questions and answers • You can write to Randal Schwartz at merlyn@stonehenge.com, or call (503)777-0095 • Or reach our other instructors via the web page http://www.stonehenge.com/perltraining/ also to get more courses like this one! • Or Larry Wall at larry@wall.org to thank him • Or post it on comp.lang.perl.misc or comp.lang.perl.moderated (after reading all the FAQs first, please!) • Or find good stuff on the web— http://www.stonehenge.com/merlyn/WebTechniques/ Randal’s WebTechniques columns http://www.stonehenge.com/merlyn/UnixReview/ Randal’s UnixReview columns http://www.stonehenge.com/merlyn/LinuxMag/ Randal’s Linux Magazine columns http://www.perl.com/ O’Reilly’s Perl Page http://www.perl.org/ Perl Monger’s info page http://www.perlsupport.com/ for commercial technical support Page 21 of 21



Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 4 (more)