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.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
● Perl reference
CS 330 Programming Languages 09 / 27 / 2007 Instructor: Michael Eckmann.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CSC3530 Software Technology Tutorial Two PERL Basics.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
Guide To UNIX Using Linux Third Edition
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Practical Extraction & Report Language PERL Joseph Beltran.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
CS 330 Programming Languages 09 / 25 / 2008 Instructor: Michael Eckmann.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Perl Practical(?)‏ Extraction and Report Language.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1 Perl Syntax: control structures Learning Perl, Schwartz.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
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 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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”;
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
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.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
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.
Scripting Languages ● Perl, Python, Unix Shell, VB, JavaScript, etc. ● Glue Languages. ● Typeless. ● Interchangeable Code and Data. ● Interpreted vs. Compiled.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Linux Administration Working with the BASH Shell.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Input from STDIN STDIN, standard input, comes from the keyboard.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Control Structures: if Conditional
Control Structures: for & while Loops
Context.
Lesson 2. Control structures File IO - reading and writing Subroutines
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
DATA TYPES AND OPERATIONS
INTRODUCTION to PERL PART 1.
Presentation transcript:

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 (Associative Arrays) Built-in Functions Miscellaneous

2/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules All scalar variables start with $ Regular array as a Element is $name[index], where index is a numeric value, default starting index is 0, define-able thru $]. $#name returns the last index of the array name ($] -1 if undefined) Hash (associative array) as a whole: %name Element of %name is $name { key }, where key is a string value The keys operator returns a regular array containing all the keys All variables are global variables, unless declared explicitly with the local operator Scalar, array, hash, file handle, and subroutine names have separate name spaces The default operand of pattern matching, IO operations, and most (but not all) of the built-in functions is $_

3/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules (cont.) It is legal to reference a variable before it gets a value assigned. When evaluated, a variable without a value assigned has the value of undefined, which will evaluate to false logically. Use the defined operator to distinguish 0(or ) and undefined Variables in … (…) will (not) be substituted with its value Same as shell interpretation Functions can be chained together, and the evaluation (following the LISP convention) started from the right and the results feed back to the left function An evaluation can produce results of different types. The exact type returned depends on the context, i.e., the data type expected by the assignment or function argument

4/12 Steven Leung Very Basic Perl Tricks File I/O Predefined file handler: STDIN, STDOUT, STDERR open(F, my_file) # Open my_file for read while ( ) {... } # Read a line thru F to $_ <> is the readline operator open(O, >my_ofile) # Open my_ofile for write print O this,, that,, whatever\n; No comma between O (the file handler) and the rest If O is omitted, the default is STDOUT printf and sprintf accept a string to specify format, same as in C E.g. printf O (%s %d, $string, $num); Pipes can be added to the filename part: open(F, pwd|);

5/12 Steven Leung Very Basic Perl Tricks Output Formatting format name $var1, $var2, $var3, $var4. # default name is STDOUT left, center, right justified fixed-point number Format statement must be ended with a. at the 1 st col. of a new line. Format is executed by the write command foreach { # set up $var1, $var2, $var3, $var4 write; } Use select(File_Handler); $~ = Format_Name ; to switch format

6/12 Steven Leung Very Basic Perl Tricks Operators, Flow Control Stmts C-Like Operators: ++, ||, &&, +=, … For numeric: ==, !=,, =, For strings: eq, ne, lt, gt, le, ge, cmp, =~ (see Reg. Expr.) File test: -e, -f, -d,... If (expr) { … } [ [elsif (expr) { … }] else { … } ] while (expr) { … } while (<>) {... } # Same as while ($_= ) {... } foreach [$var] { … } foreach (<>) {... } # Read in the whole file first for (expr; expr; expr) { … } for ($i=1; $i<=10; $i++) {... } last, next while (<>) { next if /^#/;... last if /^END/ }

7/12 Steven Leung Very Basic Perl Tricks Regular Expression Characters that have special meanings in / pattern /: () [ ]. - + * ? ^ $ \ | / Short-hand notation of often-used regular expressions: \d digit \D Not \d \s white space \S Not \s \w alphanumeric + _ \W Not \w Pattern matching operator =~ [expr =~] [m]/Pattern/[i] # Just check if pattern found [$var =~] s/Pattern/Replace/[g][i][e] # Substitution [$var =~] tr/Search_list/Repl_list/ # Translate Examples $str =~ /^\s*module\s+(\w+)/; ($cell, $inst) = $str =~ /^\s*(\w+)\s+(\S+)/; ($cell, $inst) = $str =~ /^\s*(\w+)\s+([^\s,\(]+)/;

8/12 Steven Leung Very Basic Perl Tricks Subroutines Defining a subroutine sub my_func { local($a, $b, $c) # Code to process $a $b $c return($some_val); # } Calling the subroutine my_func $ret_val = &my_func($x, $y, $z); Note that arguments ($x, $y, $z) are passed to the subroutine through the Anonymous subroutine examples foreach $n (sort {$a cmp {... } $a and $b are values passed to the anonymous sub by sort func foreach (sort {$dly{$b} $dly{$a}} keys %dly) {... } cmp/ compares by alphabetical/numeric order

9/12 Steven Leung Very Basic Perl Tricks Hash (Associative Array) Think about hash as a 2-column table keyvalue INVX117.0 AND2X125.5 SDFFX %area_of Name of the hash (table) Keys are of string typeValues are of scalar type (numeric/string) The values are accessed by $name{key} Example: $area_of{$cell_type} Function keys returns a regular array (list) of all keys Common use of hash table as a flag on an array of obj. Example: if (!$seen{$cell_type}++) {... }

10/12 Steven Leung Very Basic Perl Tricks Build-In Functions (Most Used) chop [$str] # Chop off the last char from $str split[(/pat/,$str)] split $str into an array with /pat/ as field separator # append the LIST # shift the 1 st element out grep(EXPR, LIST) Return an array of elements from LIST for which EXPR is true = grep(!$seen{$_}++, values %cell_of); warn LIST # print LIST to STDERR die LIST # print LIST to STDERR, then exit open(F, infile) || die *** Cant open infile\n; exec/system(LIST) # non-/blocking system call

11/12 Steven Leung Very Basic Perl Tricks Miscellaneous 1 st line: #!/usr/local/bin/perl Command line args are set to built-in Useful command line switches -[p|n]e perl statements Both p,n wrap the statements with while (<>) {... } -p also print $_ after the statements -i : in-place editing Use qq/q to change double/single quote character print qq|Pcap = $pincap_of{$cell_of{$inst}:$pin}\n|; Other useful special variables $0: Name of the perl script file from the command line $`, $&, $ : preceding, matching, and following strings Pattern matching of /\([^\)]+\)/ on line: abc ( 123, 321 ) xyz $` = abc, $& = ( 123, 321 ), $ = xyz

12/12 Steven Leung Very Basic Perl Tricks Version 5.2 ;... DESIGN bloom ;... COMPONENTS ; - bloom_top|cmem|..|U204 ANDX1 ; - bloom_top|regs|..|U31 XOR2X2 ;... END COMPONENTS... bloom.def Exercise Write the get_cell_stat script for the following input/output % get_cell_stat bloom.def > bloom.cell_stat Cell Count Perc Acc NANDX INVX bloom.cell_stat get_cell_stat