COMP234 Perl Printing Special Quotes File Handling.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Advertisements

Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 4: Introduction to Perl and CGI programming. PERL -- Practical Extraction and Report Language Developed in 1980's by Larry Wall He created it to.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Introduction to Python
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
Guide To UNIX Using Linux Third Edition
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Python
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Input, Output, and Processing
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
1 Lecture 5 Additional useful commands COP 3353 Introduction to UNIX.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
5 1 Data Files CGI/Perl Programming By Diane Zak.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Files Tutor: You will need ….
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
THE C PROGRAMMING ENVIRONMENT. Four parts of C environment  Main menu  Editor status line and edit window  Compiler message window  “Hot Keys” quick.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
Fundamentals of Programming I Overview of Programming
Input from STDIN STDIN, standard input, comes from the keyboard.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Programming in C Input / Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Topics Introduction to File Input and Output
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Topics Introduction to File Input and Output
Introduction to C Programming
Presentation transcript:

COMP234 Perl Printing Special Quotes File Handling

Perl Documentation Can be found here: – The bulk of the reference material is in: – You can find that link under Reference/Functions Also important: –Language –Operators –Special Variables

Working with Perl Edit the source code with a text editor (not a word processor) Don't copy snippets from word documents Use three windows –A command prompt, cd to working dir –Your text editor –A file manager viewing your working directory You can usually drag and drop file to editor –And to the command prompt

Functions in perl In perl, everything is a function –Even things that look like commands Everything returns a value Functions can have effects other than just returning a value or changing the argument values Parentheses are optional

Interpolation When using single quotes all characters are treated as literals. When using double quotes, almost all characters are treated as literals. But some things are not. These exceptions are “interpolated” into the rest of the literal. –Escape sequences –Variables

Variable Substitution Example: $Word1 = "thank"; $Word2 = "you"; $Sentence = "I just wanted to say $Word1 $Word2."; print $Sentence; Program Output: I just wanted to say thank you.

Variable Substitution Program Output: –I just wanted to say thank you. Perl evaluates each variable and interpolates it directly into the literal Notice that when the print statement displays the contents of $Sentence, it inserts a space between the two quoted words and inserts the period at the end. Perl evaluates the variables and interpolates the values but leaves other characters, such as spaces, exactly as you enter them.

Another Built-In Variable We looked at $_, $, $/ $” Variables that start with $ are scalars –Single valued Variables that start are arrays –A list or table of is an array of file system paths Each path is the location of a perl library that will be searched at run time to find included perl modules

perl -e perl has the useful ability to run short programs without first saving them to a file perl -e “text” –runs text as a perl program perl -e “print 2 + 2” –Prints 4

Perl -e "print E:/PortableApps/perl/perl/lib is interpolated into double quoted string $” is inserted between array elements $” contains a space The \ tells DOS to ignore the following quote Otherwise the program would end at the second quote Actual program is: print

printf Example: printf("The name is: %10s\nThe number is: %8.2f\n","Ellie", 33); The printf function prints a formatted string Comma separated arguments are substituted into the string before printing. Perl uses a "format specifier" character ("%x") in order to specify how to print the variable value when it has been substituted. For example: %s = string %f = floating point number %c = character

printf You can also specify "flag modifiers": %10s = right justified 10 character string %8.2f = right justified 8 character floating point number where max field length is 8 characters, includes a character for a decimal point, followed by 2 digits. Example: printf("The name is: %10s\nThe number is: %8.2f\n","Ellie", 33); Program Output: The name is:Ellie The number is:33.00 Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.

Backticks Strings enclosed in backticks are treated as DOS commands String is executed in a dos shell Result is interpolated in place of the string See Operators, search for backticks Example: print "$str\n"; print

OUTPUT Volume in drive D is HONKER2 Volume Serial Number is DB Directory of D:\Perl\my.pl 01/22/ :49a. 01/22/ :49a.. 01/22/ :53a 72 test.pl 01/22/ :54a 0 test.out 2 File(s) 72 bytes 2 Dir(s) 1,442,611,200 bytes free

Here Documents A way of specifying multi-line literals See operators, search for Here Document Example: $str =<<EOF This is a long litteral It is three lines long This is the third line EOF print $str This is a long litteral It is three lines long This is the third line

Here Documents If the terminating string is quoted, the type of quotes used determine the treatment of the text. Double Quotes –Double quotes indicate that the text will be interpolated using exactly the same rules as normal double quoted strings.

Here Documents Single Quotes –Single quotes indicate the text is to be treated literally with no interpolation of its content. Backticks –The content of the here doc is treated just as it would be if the string were embedded in backticks. Thus the content is interpolated as though it were double quoted and then executed via the shell, with the results of the execution returned.

Examples $str=<<EOF; A line to print\n “EOF” print $str $str=<<EOF; A line to print\n 'EOF' print $str

Examples $str=<<EOF; dir `EOF` print $str Try these in the lab

File Processing

You must open a file before you can process it Opening associates a FILEHANDLE with a file Outside the perl program files are known by their paths – c:\myprograms\mytestdata.txt Inside a perl program an open file is known by its FILEHANDLE

Open Function open FILEHANDLE,EXPR open FILEHANDLE,MODE,EXPR open FILEHANDLE... Lots of format options You will normally use: –open FILEHANDLE,MODE,EXPR Or –open FILEHANDLE,EXPR

Open Function open FILEHANDLE,MODE,EXPR FILEHANDLE is an identifier of your choice, conventionally all upper case: – INPUT MYFILE SALES MODE indicates intended usage – “<” -- input – “>” -- output (Creates file or overwrites) – “>>” -- append EXPR is the path to the file, or a variable

Open Function Variations open FILEHANDLE,EXPR MODE can be omitted, –default is “< “meaning input Or mode can be the first character in expression –Open OUTPUT, “> c:\data\output.txt”; FILEHANDLE can be contained in a variable –Open $handle, $filename

Error Handling When dealing with files: – Always be prepared for the unexpected – You can't control the state of the disk when the program is run – So you have to be prepared for non-existent files

Error Handling Die function: –die LIST – Prints the value of LIST to STDERR and exits with the current value of $! (errno). Exiting with die is the normal means of handling file processing errors There is usually nothing else you can do

ERRNO $! if a system or library call fails, it sets this variable See Perl special variables – $! returns different value depending on context If die $! yields –“No such file or directory at files.pl line 2.” die abs($!) yields –“2 at files.pl line 2.”

Die Variations With or without line feed: die $! yields –No such file or directory at files.pl line 2. Die “$!\n” yields –No such file or directory If you don't specify \n, –perl adds “at $program_name line $line_number \n”

Open with Error Handling open MYINPUT, “<input.txt” or die “Can't open input.txt”, $!; Opens file or prints message and quits. Quitting is the usual response to file errors

open or die Variations “||” instead of “or”: or and || both mean “or” but “or” has higher precedence open IN, “file” || die $!; doesn't work It is interpreted as –open IN, (“file” || die $!) open IN, “file” or die $!; does work It is interpreted as –(open( IN, “file”) or die $!)

Working with files In scalar context, evaluating a filehandle in angle brackets yields the next line from that file $line = You can assign the entire file to an array = And then print it – print

Lab this week A simple lab to explore the perl working environment and create a hello world program