CSE 341 S. Tanimoto Perl Perl: User Defined Functions sub funny_print { my ($name, $age) print <<END_FUNNY_PRINT; The person\’s name is $name,

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Programming and Perl for Bioinformatics Part III.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Regular Expressions in Java. Namespace in XML Transparency No. 2 Regular Expressions Regular expressions are an extremely useful tool for manipulating.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Regular Expressions in Java. Regular Expressions A regular expression is a kind of pattern that can be applied to text ( String s, in Java) A regular.
CSE S. Tanimoto Regular Expressions 1 Regular Expressions: Theory and Perl Implementation Outline: 1. Theoretical Definitions and Examples 2. Acceptance.
Scripting Languages Chapter 8 More About Regular Expressions.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
More on Regular Expressions Regular Expressions More character classes \s matches any whitespace character (space, tab, newline etc) \w matches.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Lecture 8 perl pattern matching features
Regular Expressions in Perl Part I Alan Gold. Basic syntax =~ is the matching operator !~ is the negated matching operator // are the default delimiters.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Regular Expressions Regular Expressions. Regular Expressions  Regular expressions are a powerful string manipulation tool  All modern languages have.
Module 6 – Generics Module 7 – Regular Expressions.
Python for NLP Regular Expressions CS1573: AI Application Development, Spring 2003 (modified from Steven Bird’s notes)
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Finding substrings my $sequence = "gatgcaggctcgctagcggct"; #Does this string contain a startcodon? if ($sequence =~ m/atg/) { print "Yes"; } else { print.
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CMSC330 More Ruby. Last lecture Scripting languages Ruby language –Implicit variable declarations –Many control statements –Classes & objects –Strings.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Java Basics Regular Expressions.  A regular expression (RE) is a pattern used to search through text.  It either matches the.
Chapter 8 – Regular Expressions
Introduction to Perl: Part II
System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 References: Perl man pages Albert Lingelbach, Jr.
Perl: Functions, References, Etc.
Perl Variables: Array Web Programming.
Perl: Arrays, Functions, References, Etc.
Perl: Arrays, Functions, References, Etc.
Context.
Perl Functions.
Lesson 2. Control structures File IO - reading and writing Subroutines
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
CSCI 431 Programming Languages Fall 2003
Perl: Arrays, Functions, References, Etc.
Perl: Arrays, Functions, References, Etc.
Regular Expressions: Theory and Perl Implementation
Shell Control Structures
Perl: Arrays, Functions, References, Etc.
Shell Control Structures
Regular Expressions: Theory and Perl Implementation
Java: Variables, Input and Arrays
Perl: Arrays, Functions, References, Etc.
Regular Expressions: Theory and Perl Implementation
INTRODUCTION to PERL PART 1.
Presentation transcript:

CSE 341 S. Tanimoto Perl Perl: User Defined Functions sub funny_print { my ($name, $age) print <<END_FUNNY_PRINT; The person\’s name is $name, and the ages is $age. END_FUNNY_PRINT return 1; } funny_print ("Steve", 50); # or &funny_print("Steve", 50); funny_print "Steve", 50; # or # This last form is OK AFTER the defn.

CSE 341 S. Tanimoto Perl Fetching Function Args w/ shift sub add1 { my $number = shift; return $number + 1; } add1(5); # shift removes and returns the first # element of an array, and the default # array

CSE 341 S. Tanimoto Perl Packages #!/usr/bin/perl -w Package OakTrees; sub getseeds { print "acorns"; } Package PalmTrees; sub getseeds { print "coconuts" ; } getseeds() ; # prints coconuts; OakTrees::getseeds(); # prints acorns PalmTrees::getseeds(): # prints coconuts # A package is a separate namespace for # functions and variables. # To cross packages use fully-qualified name

CSE 341 S. Tanimoto Perl Modules #!/usr/bin/perl -w use MapleTreesModule; # The compiler searches for MapleTreesModule.pm # following all the paths in an array # The module is a precompiled package of the same name. # When the “use” is executed, all the definitions in the # package become available, and any other code in # the package gets executed. # Packages can be in subdirectories of searched paths: use Net::Mail; # Mail.pm must be in Net.

CSE 341 S. Tanimoto Perl References (smart pointers) A reference is a pointer that is used to access some Perl data or function. There is a reference count for each item. $scalarref = \$scalar; $five = 5; $fiveref = \$five; print 1 + $$fiveref; # prints 6.

CSE 341 S. Tanimoto Perl References (continued) Local variables persist even if execution exits their scope if their reference count is greater than zero. (I.e., local variables can have indefinite extent.) References can be used for scalars, arrays, hashes, functions and other references. $scalar = "A Scalar"; $hash{"KEY"} = "A hash entry"; $scalarref = \$scalar; $hashref = \%hash;

CSE 341 S. Tanimoto Perl Anonymous Variables sub someTrees { = qw(oak pine elm); return } $treesRef = someTrees(); sub moreTrees { ["banyan”, "beech"]; # anonymous } # both functions return references.

CSE 341 S. Tanimoto Perl Regular Expressions in Perl $sentence = "Spring weather has arrived." if ($sentence =~ /weather/) { print "Never bet on the weather." ; } # $string =~ /Pattern/ The result of this kind of pattern matching is a true or false value.

CSE 341 S. Tanimoto Perl Specifying Patterns /Pattern/ # Literal text; # true if it occurs anywhere in the string. /^Pattern/ # Must occur at the beginning. "Pattern recognition is alive" =~ /^Pattern/ "The end" =~ /end$/ \s whitespace \S non-whitespace \w a word char. \W a non-word char. [a-zA-Z_0-9] \d a digit \D a non-digit \b word boundary \B not word boundary

CSE 341 S. Tanimoto Perl Specifying Patterns (Cont.) $test = "You have new mail "; if ($test =~ /^You\s.+\d+-\d+-\d+/ ) { print "The mail has arrived."; } if ($test =~ m( ^ You \s.+ \d+ - \d+ - \d+ ) { print "The mail has arrived."; }

CSE 341 S. Tanimoto Perl Extracting information $test = "You have new mail "; if ($test =~ /^You\s.+(\d+)-(\d+)-(\d+)/ ) { print "The mail has arrived on "; print "day $2 of month $1 in year $3.\n"; } # Parentheses in the pattern establish # variables $1, $2, $3, etc. to hold # corresponding matched fragments.

CSE 341 S. Tanimoto Perl Search and Replace $sntc = "We surfed the waves the whole day." $sntc =~ s/surfed/sailed/; print $sntc; # We sailed the waves the whole day. $sntc =~ s/the//g; print $sntc; # We sailed waves whole day. # g makes the replacement “global”.

CSE 341 S. Tanimoto Perl Interpolation of Variables in Patterns. $exclamation = "yeah"; $sntc = "We had fun." $sntc =~ s/\w+/$exclamation/g; print $sntc; # yeah yeah yeah. # a pattern can contain a Perl variable.