CS105 Computer Programming Perl - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS105 Computer Programming Perl

Description:

Pathologically Eclectic Rubbish Lister. Practically Everything Really Likable ... Execute as a script/shell program. Execute as a Shell Program ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 12
Provided by: peteryeh
Category:

less

Transcript and Presenter's Notes

Title: CS105 Computer Programming Perl


1
CS105 Computer Programming Perl
2
What is PERL?
  • Practical Extraction Report Language
  • Pathologically Eclectic Rubbish Lister
  • Practically Everything Really Likable
  • (about the shells, awk, sed, grep, and C)
  • Written by Larry Wall
  • Perl is a GNU product!.
  • Perl borrows ideas from all over the place.
  • Perl runs on many platforms.

3
What is PERL?
  • Perl is not an acronym it is what we call a
    backronym
  • Objective Making Easy Things Easy and Difficult
    Things Possible
  • Motto TMTOWTDI There's More Than One Way To Do
    It

4
Why use PERL?
  • Easy - for daily use
  • Nearly unlimited - for high-level programming (no
    real time, no kernel, no device drivers please)
  • Mostly fast - fast enough for the tasks you would
    do with Perl
  • Not too nice - the symbol of Perl is the camel,
    ugly but can handle difficult environment

5
Who uses PERL?
  • System administrators
  • Many other UNIX users.
  • Data Miners
  • Database Engineers
  • Biologist
  • You!

6
Setting Up a CS Account.
  • Get an account from the CS department
  • CS department offers guest account
  • Do all assignments on CS Unix machines
  • Environment in which assignments will be graded
  • CS account comes with webspace
  • Needed for some assignments

7
Running Perl in Unix
  • Three ways to run a perl program
  • Execute program stored in a file
  • gt perl myfile.pl
  • Execute as a single statement
  • gt perl e print Hello World.\n
  • Useful in determining if a statement will work.
  • Execute as a script/shell program.

8
Execute as a Shell Program
  • First line in the file must tell the OS where
    Perl is located
  • !/usr/bin/perl
  • Each statement must end with a semicolon ().
  • In some cases the statement must be on one line.
  • Comments begin with and everything is case
    sensitive.
  • Must tell OS that the file is executable.
  • gt chmod 700 myfile.pl
  • gt myfile.pl mydata.txt

9
Some Useful Options
  • By default, Perl wont give any warnings for
    syntax or programming errors
  • This makes catching bugs hard
  • Check but dont execute
  • gt perl c myfile.pl
  • Useful for finding syntax errors.
  • Check for common mistakes
  • gt perl w myfile.pl
  • Check for common programming errors and
    suspicious code.

10
Quick and dirty (EX - replace file content)
  •   You have a bunch of files in your directory ment
    ioning the name        "President Bush"    Yo
    u are told to replace that by         "President
     Reagen"    perl -i -p -e "s/Bush/Reagen/g"
     

11
Quick and dirty(EX - process a csv file)
  • You have a number of csv files, you want to print 
    the 3rd field of each row of each file.    perl
    -F, -ane 'print "F2\n"' .csv
  • You want to make sure all the rows are 4 elements 
    long. Print out file name and line number of all 
    the bad rows.    perl -F, -ane 'print
    "ARGV.\n" if _at_F ! 4' .csv 
Write a Comment
User Comments (0)
About PowerShow.com