Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
A Guide to Unix Using Linux Fourth Edition
● Perl reference
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Practical Extraction & Report Language Picture taken from
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Perl Programming WeeSan Lee
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Lists in Python.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Continuing with Perl Homework 3: first Perl homework – due Sunday by midnight – one PDF file, by .
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
Introduction to Perl Yupu Liang cbio at MSKCC
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
ITM © Port, KazmanVariables - 1 ITM 352 Data types, Variables.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Getting started in Perl: Intro to Perl for programmers Matthew Heusser – xndev.com - Presented to the West Michigan Perl User’s Group.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
ITM © Port, KazmanVariables - 1 ITM 352 Data types, Variables Class #4.
The Scripting Programming Language
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
ITM 352 Data types, Variables
Perl Variables: Array Web Programming.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
Perl I/O Learning Objectives:
Presentation transcript:

perl Basics

sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”; print “Hello, world!\n”; new line all three lines do the same thing # - shell ! – bang executing perl hello.pl without sh-bang line is the same as./hello.pl with the sh-bang line must be first line in file Question: printf is a function; what is strange about its appearance? NOTE: Don't forget to chmod +x hello.pl

Variables: perl has three kinds of variables: –scalar ($-prefix)‏ -- numerics, strings, addresses, primitives –list/array -- lists, arrays, stacks, queues –hashtable (%-prefix)‏ -- key/value pairs Typical symbol table: perl symbol table: Variable Name Address X 0x Variable Name VariableType Address X 0x not defined 0x typeglob

Scalars and Lists: The following is possible: In other words, two variables with the same name $X = = ('apple', 1, 'orange', 2, 'pear', 3);

Scalar Variables: numeric, string, boolean, address are all scalars $inum = 2; # numeric $fnum = 2.0; # numeric $fname = “Andrew”; # string $lname = “Pletch; # string $name1 = “$fname $lname”; # value of $name1 is “Andrew Pletch” $name2 = ‘$fname $lname’; # value of $name2 is “$fname $lname” if ($inum == $fnum) { … } # numeric comparison (true)‏ if ($name1 eq $name2) { … } # string comparison $snum = “2”; # string if ($inum == $snum) { … } # a string whose value is numeric # can be treated like a number double quotes expands variables single quotes leaves variables unexpanded.

eq == #!/usr/bin/perl $X = 2; $Y = 2.0; $Z = "2"; $W = “2.0”; if ( $X == $Y ) { printf "int == float\n"; } if ( $X eq $Y ) { printf "int $X eq float $Y\n"; } if ( $X == $Z ) { printf "int == string\n"; } if ( $X eq $Z ) { printf "int eq string\n";} if ( $Z == $Y ) { printf "string $Z == float $Y\n"; } if ( $Z eq $Y ) { printf "string eq float\n"; } if ( $X == $W ) { printf "int == string\n";} if ( $Z == $W ) { printf “string == string\n";} if ( $Z ne $W ) { printf “string ne string\n";} # output int == float int 2 eq float 2 int == string int eq string string 2 == float 2 string eq float int == string string == string string ne string

List Variables: Lists are lists of scalars. Notice that the things in a list don't have to be the same sort of thing as in other programming languages -why not? List elements are accessed using [ ]. If $X = “apple” then $X[2] can not be 'p'; why = ('apple', 1, 'orange', 2, 'pear', 3); $L[0] is a fruit; $L[1] is a number. We use $ because 'apple' is a scalar Because there might also be and so $X[2] must be the third element in this list; use substr($X,2,1) instead.

Exercise: The perl substr() function is a whole study in itself; take a look at

List Variables 2: Suppose we execute then inside is a predefined variable whose value is To access the elements we use indexes > myprog.pl -t -a 3 –f myfile ( “-t”, “-a”, 3, “-f”, “myfile”)‏ $FName = $ARGV[4]; # indexing starts at 0 use $ because “myfile” is a scalar use ( ) to declare or express a list

List Variables 3: A list of lists is just a list: A list used in a scalar context returns the list = ( ( 1, 2, 3), (4, 5, 6), (7, 8, 9) ); # looks like a 3x3 matrix # value is ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); # but it isn’t “\n”; # scalar context printf \n”; # list context output: string concatenation operator

hash variables: %ENV is a predefined hash variable; it contains a copy of all the environment variables and their values inherited by any running perl script #!/usr/bin/perl # prints out all inherited environment variables # and their values foreach $key (keys %ENV) { printf "$key ==> $ENV{$key} \n"; } function that returns a list of all keys to the hash for-loop syntax for lists [ ] for lists; { } for hashes ( ) part of for-loop syntax and not part of list definition; required

Perl hash HowTo:

hashes and lists: #!/usr/bin/perl #hash_test.pl %myHash; $myHash{A} = 1; $myHash{B} = 2; $myHash{C} = 3; $myHash{D} = 4; print %myHash; print %myHash. "\n"; print "%myHash = %myHash; "\n"; printf \n"; printf "ENV == %ENV \n"; # output A1B2C3D4 3/8 %myHash 8 A 1 D 4 C 3 B 2 ENV == E+00NV using hash in scalar context printing a hash does nothing but treat it like a string assign hash to list 3 == number of used buckets 8 == number of allocated buckets Prints out the contents all mashed together %E treated like 0.0E

perl symbol tables: hash consisting of (key, value) pairs key == identifier value == typeglob data structure typeglob contains reference to all three variables %x; # three variables with same name # %main is the default package symbol table called main # $main{x} is a typeglob that accesses all three variables # *main::x is the same typeglob

on-line help: /perl/howto/hash/ /perl/howto/hash/