1.1.2.8 – Intermediate Perl 10/23/20151.1.2.8.1 - Intermediate Perl - References 1 1.1.2.8.2 Intermediate Perl Session 2 · references, continued · function.

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

Advanced Perl WUC Perl Poetry Variable Scope.
Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
Subroutines sub { –#parameters are placed – –. –return; }
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
PHP : Hypertext Preprocessor
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
subroutines and references
– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
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.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
– Intermediate Perl 11/19/ Intermediate Perl - debugging, benchmarking and best practices Intermediate Perl – Session.
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
References. Analagous (somewhat) to pointers in C/C++ –Far less messy, and definitely less dangerous –Variables that "refer" to another variable You can.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.
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.
– Intermediate Perl 2/1/ Intermediate Perl - sort/grep/map Intermediate Perl – Session 3 · map · transforming data ·
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Intro to Object Oriented Perl Packages, Modules, Classes.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
PERL SCRIPTING. COMPUTER BASICS CPU, RAM, Hard drive CPU can only use data in the register directly CPU RAM HARD DRIVE.
– sort/grep/map in Perl 6/14/ A closer look at sort/grep/map Perl’s sort/grep/map · map · transforming data · sort.
Built-In Functions. Notations For each function, I will give its name and prototype. –prototype = number and type of arguments ARRAY means an actual named.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
References and Data Structures
Introduction to Perl: Part II
Intermediate Perl - References
Perl Variables: Array Web Programming.
Subroutines Web Programming.
References and Objects
The Selection Structure
Subroutines.
Presentation transcript:

– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function references · dispatch tables · Data::Dumper

– Intermediate Perl 10/23/ Intermediate Perl - References 2 = (1,2,3) hash %h = (one=>1,two=>2) reference created from variable $a_ref = = \%h anonymous reference$a_ref = [1,2,3]$h_ref = {one=>1,two=>2} dereferencing $a_ref->[0] $a_ref->[1] $a_ref->[2] $h_ref->{one} $h_ref->{two} Reference table to references

– Intermediate Perl 10/23/ Intermediate Perl - References 3 Writing functions that return list references · we want to make a list of the first N perfect squares # function returns = create_squares(100); for $square { print $square,"\n"; } sub create_squares { $n = = (); for $i (1..$n) { $i**2; } } # function returns list reference # and internally works with list # reference $squares = create_squares(100); for $square { print $square,"\n"; } sub create_squares { $n = shift; $x = []; for $i (1..$n) { $i**2; } return $x; } # function returns reference # but internally works with list $squares = create_squares(100); for $square { print $square,"\n"; } sub create_squares { $n = = (); for $i (1..$n) { $i**2; } return }

– Intermediate Perl 10/23/ Intermediate Perl - References 4 Writing functions that return hash references · we want to make a hash of first N letters and their alphabet position · key is letter, value is order in alphabet # function returns hash %letters = create_letters(20); for $letter (sort keys %letters) { print $letter,$letters{$letter},"\n" } sub create_letters { $n = shift; %h = (); $n = 26 if $n > 26; $i = 1; for $let ( (a..z)[0..$n-1]) { $h{$let} = $i++; } return %h; } a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j # function returns hash # a little tidier %letters = create_letters(20); for $letter (sort keys %letters) { print $letter,$letters{$letter},"\n" } sub create_letters { $n = shift; %h = (); $n = 26 if $n > 26; for $let ( (a..z)[0..$n-1]) { $h{$let} = 1 + scalar keys %h; } return %h; }

– Intermediate Perl 10/23/ Intermediate Perl - References 5 Writing functions that return hash references · we want to make a hash of first N letters and their alphabet position · key is letter, value is order in alphabet a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j # function returns hash %letters = create_letters(20); for $letter (sort keys %letters) { print $letter,$letters{$letter},"\n" } sub create_letters { $n = shift; %h = (); $n = 26 if $n > 26; for $let ( (a..z)[0..$n-1]) { $h{$let} = 1 + keys %h; } return %h; } # function returns hash reference $letters = create_letters(20); for $letter (sort keys %$letters) { print $letter,$letters->{$letter},"\n" } sub create_letters { $n = shift; $h = {}; $n = 26 if $n > 26; for $let ( (a..z)[0..$n-1]) { $h->{$let} = 1 + keys %$h; } return $h; }

– Intermediate Perl 10/23/ Intermediate Perl - References 6 writing functions that modify referents · you can pass a reference to a function in order to · access or modify the referent # reference to list of squares $squares = create_squares(100); # print squares print_squares($squares); # format list in place to add decimal format_squares($squares); sub print_squares { $sref = shift; for $s { print $s; } sub format_squares { $sref = shift; for $i { $sref->[$i] = sprintf("%.1f",$sref->[$i]); } # this also works – why? sub format_squares { $sref = shift; for $s { $s = sprintf("%.1f",$s); } # but not this – why? sub format_squares { $sref = shift; for $s { $square = $s; $square = sprintf("%.1f",$square); }

– Intermediate Perl 10/23/ Intermediate Perl - References 7 diagnosing data structures with Data::Dumper · use Dumper() to create stringified data structure · man Data::Dumper to learn more sub create_letters { $n = shift; $h = {}; $n = 26 if $n > 26; for $let ( (a..z)[0..$n-1]) { $h->{$let} = 1 + keys %$h; } return $h; } use Data::Dumper; $letters = create_letters(20); print Dumper($letters); $VAR1 = { 'e' => 5, 'a' => 1, 'd' => 4, 'j' => 10, 'c' => 3, 'h' => 8, 'b' => 2, 'g' => 7, 'f' => 6, 'i' => 9 }; # Sortkeys is one of the # customizable parameters # in the module $Data::Dumper::Sortkeys = 1; print Dumper($letters); $VAR1 = { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10 };

– Intermediate Perl 10/23/ Intermediate Perl - References 8 diagnosing data structures with Data::Dumper · let's start to build a more complicated structure $letters = create_letters(6); print Dumper($letters); sub create_letters { $n = shift; $h = {}; $n = 26 if $n > 26; for $let ( (a..z)[0..$n-1]) { # value associated with the letter # is now a hash reference $h->{$let} = {idx => 1 + keys %$h}; # could also write this as # $h->{$let}{idx} = 1 + keys %$h; } return $h; } $VAR1 = { 'a' => { 'idx' => 1 }, 'b' => { 'idx' => 2 }, 'c' => { 'idx' => 3 }, 'd' => { 'idx' => 4 }, 'e' => { 'idx' => 5 }, 'f' => { 'idx' => 6 }, };

– Intermediate Perl 10/23/ Intermediate Perl - References 9 diagnosing data structures with Data::Dumper · let's start to build a more complicated structure $letters = create_letters(3); for $letter (keys %$letters) { # ord key will store the ASCII value, e.g. a = 97 $letters->{$letter}{ord} = ord($letter); # reverse key will store corresponding letter from end of alphabet $letters->{$letter}{reverse} = (reverse a..z)[ $letters->{$letter}{idx} -1 ]; # prev is a list reference of all letters before $letter $letters->{$letter}{prev} = [ (a..z)[ 0.. $letters->{$letter}{idx} - 2 ]; # next is a list reference of all letters after $letter $letters->{$letter}{next} = [ (a..z)[ $letters->{$letter}{idx}.. 25 ]; } 'c' => { 'idx' => 3, 'next' => [ 'd', 'e', 'f',... 'x', 'y', 'z' ], 'ord' => 99, 'prev' => [ 'a', 'b' ], 'rev' => 'x' }

– Intermediate Perl 10/23/ Intermediate Perl - References 10 diagnosing data structures with Data::Dumper · use a temporary variable to store the current hash value $letters = create_letters(3); for $letter (keys %$letters) { # store the value (which is a hash reference) in a temporary variable $h = $letters->{$letter}; # now use the temporary variable to make code more succinct $h->{ord} = ord($letter); $h->{reverse} = (reverse a..z)[ $h->{idx} -1 ]; $h->{prev} = [ (a..z)[ 0.. $h->{idx} - 2 ]; $h->{next} = [ (a..z)[ $h->{idx}.. 25 ]; }

– Intermediate Perl 10/23/ Intermediate Perl - References 11 some common error messages · here are the kinds of error messages you will see if you try to dereference non- references or the wrong references $x = "sheep"; $x->[0] = 1; # nope, you can't dereference a non-reference Can't use string ("sheep") as an ARRAY ref while "strict refs" in use at./somescript line 10. $x->{a} = 1; # nope, you can't dereference a non-reference Can't use string ("sheep") as a HASH ref while "strict refs" in use at./somescript line 10. $x = [1]; # now $x is a list reference $x->{a} = 1; # try to treat it like a hash reference Can't coerce array into hash at./somescript line 10. $x = {sheep=>1}; # now $x is a hash reference $x->[0] = 1; # try to treat it like an array reference Not an ARRAY reference at./somescript line 10. $x = {sheep=>1}; $x->{sheep} = "meeh"; $x->{sheep}[0] = 2; Can't use string ("meeh") as an ARRAY ref while "strict refs" in use at./somescript line 11.

– Intermediate Perl 10/23/ Intermediate Perl - References 12 Freezing data structures with Storable · the Storable module allows you to · store/retrieve data structures to/from disk # script 1 use Storable; $letters = create_letters(26); # store(REFERENCE,FILE) store($letters,"letters.cache"); # script 2 use Storable; # retrieve(FILE) $letters = retrieve("letters.cache"); # now use $letters as desired # the script -rwxr-xr-x 1 martink users 424 Jul 29 13:26 letterorder # the cache file -rw-r--r-- 1 martink users 540 Jul 29 13:26 letters.cache

– Intermediate Perl 10/23/ Intermediate Perl - References 13 function references – the final reference frontier · just like with lists and arrays, you can have a reference to a function · the sigil for functions is & · the dereference syntax is ->(ARGUMENTS) sub square { my $num = shift; return $num**2; } print square(2); 4 my $square_ref = \□ print $square_ref; CODE(0x818d9e4) print $square_ref->(2); 4

– Intermediate Perl 10/23/ Intermediate Perl - References 14 creating dispatch tables – from strings to actions · you can store references to functions in a hash sub square { my $num = shift; return $num**2; } sub root { my $num = shift; return sqrt($num); } # create a hash of functions # this is called a dispatch table my $functions = { do_root => \&root, do_square => \&square }; # call functions by name (i.e. by key value) $functions->{do_root}(2) $functions->{do_square}(2) 4

– Intermediate Perl 10/23/ Intermediate Perl - References 15 anonymous function references · sub{} actually returns the code reference to the function $do_square = sub { return $_[0]**2 }; $do_root = sub { return sqrt($_[0]) }; # create a hash of functions # this is called a dispatch table my $functions = { do_root => $do_root, do_square => $do_square }; # call functions by name (i.e. by key value) $functions->{do_root}(2) $functions->{do_square}(2) 4

– Intermediate Perl 10/23/ Intermediate Perl - References 16 function templates · you can write functions that return function references · sometimes called "factories" · disregard the "my" for now – this is a variable scoping thing we'll see soon sub power_factory { my $power = shift; my $fn = sub { return $_[0]**$power }; return $fn; } $do_square = power_factory(2) $do_root = power_factory(0.5); $do_5 = power_factory(5); print $do_square->(2) 4 print $do_root->(3) print $do_5->(4) 1024

– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · use Data::Dumper to visualize data structures · create function references with \&FUNCTION · create function references with $ref = sub { }