Intermediate Perl - References

Slides:



Advertisements
Similar presentations
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Advertisements

CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
subroutines and references
– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres.
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introduction to Perl Yupu Liang cbio at MSKCC
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.
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.
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.
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.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Python Let’s get started!.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Perl Backticks Hashes printf Variables Pragmas. Backticks.
Chapter Goals Describe the application development process and the role of methodologies, models, and tools Compare and contrast programming language generations.
Lecture 3 Translation.
Web Database Programming Using PHP
References and Data Structures
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Introduction to Perl: Practical extraction and report language
Software Development.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
C-Shell with Functions
Input from STDIN STDIN, standard input, comes from the keyboard.
Learning to Program D is for Digital.
Python Let’s get started!.
Formatting Output.
Web Database Programming Using PHP
DBW - PHP DBW2017.
Functions and Procedures
Debugging and Random Numbers
Variables, Expressions, and IO
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl Modules.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
Intro to PHP & Variables
User Defined Functions
Advanced Topics Web Programming.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Topics Introduction to File Input and Output
Subroutines Web Programming.
Object-Oriented Programming Using C++ Second Edition
Programming Tips GS540 January 10, 2011.
Lesson 2. Control structures File IO - reading and writing Subroutines
Chapter Four UNIX File Processing.
AKA LET'S WRITE SOME FUCKING PERL by William Orr
Basics (Cont...).
References and Objects
Lesson #6 Modular Programming and Functions.
Mastering Memory Modes
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Subroutines.
Topics Introduction to File Input and Output
References Revisted (Ch 5)
ITM 352 Functions.
Presentation transcript:

1.1.2.8.2 - Intermediate Perl - References Intermediate Perl – Session 2 references - continued code references persistance data caching symbolic references a few words against their use 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Last Time \$ \@ \% () [] {} 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Code References we have seen scalar, array and hash references code references point to subroutines – code you can actually execute remember that & is the sigil for a subroutine sub peelme { print shift,"is peeled"; } peelme("banana"); my $peelme_ref = \&peelme; print $peelme_ref; &{$peelme_ref}("papaya"); &$peelme_ref("another papaya"); $peelme_ref->("another banana"); banana is peeled CODE(0x80cdaa8) papaya is peeled another papaya is peeled another banana is peeled 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Reference Syntax To-Date scalar $x array @a hash %h functions &f reference $x_ref = \$x $a_ref = \@a $h_ref = \%h $f_ref = \&f dereference $$x_ref @$a_ref $a_ref->[ ] %$h_ref $h_ref->{ } &$f_ref $f_ref->( ) 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Creating Anonymous Subroutines Just like [ ] and { }, you can create anonymous subroutines with sub { } Think of sub { } as an operator, not a declaration – therefore need ; at end my $square = sub { $_[0]**2 }; print $square->(2); print $square->(3); 4 9 declaration operator sub square { $_[0]**2; } print square(2); print square(3); my $square = sub { $_[0]**2 }; print $square->(2); print $square->(3); 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Reference Syntax Table list (1,2,3) hash (one=>1,two=>2) functions sub square { $_[0]**2 } regular reference @a = (1,2,3) $a_ref = \@a %h = (one=>1,two=>2) $h_ref = \%h sub square { $_[0]**2;} $square_ref = \□ anonymous $a = [1,2,3]; $h = {one=>1,two=>2}; $square = sub {$_[0]**2}; 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Uses of Anonymous Subroutines use anonymous subroutines to make a list of functions decide which function to run at run-time great for sorts my @list = qw(1 2 3 4 a b c 10 11 q 12 01); my $sort_by_num = sub { $a <=> $b }; my $sort_by_string = sub { $a cmp $b }; my $sort_by_shuffle = sub { rand() <=> rand() }; print sort $sort_by_num @list; print sort $sort_by_string @list; print sort $sort_by_shuffle @list; Argument "a" isn't numeric in ncmp at ./coderef line 54. Argument "b" isn't numeric in ncmp at ./coderef line 54. Argument "c" isn't numeric in ncmp at ./coderef line 54. Argument "q" isn't numeric in ncmp at ./coderef line 54. q b a c 1 01 2 3 4 10 11 12 01 1 10 11 12 2 3 4 a b c q q 2 b 12 a 10 01 11 c 1 4 3 my $sort_by_num_safe = sub { ($a =~ /\D/ || $b =~ /\D/) ? $a cmp $b : $a <=> $b }; print sort $sort_by_num_safe @list; # 01 1 2 3 4 10 11 12 a b c q 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Flexible Sorting sometimes you don’t know what you’re about to sort and you want to do it properly we’ll cover more funky sorting at another time my @list = qw(1 2 3 4 a b c 10 11 q 12 01); if(grep($_ =~ /\D/, @list)) { print sort $sort_by_string @list; } else { print sort $sort_by_num @list; } 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Function Templates generate functions which behave similarly by transforming a single declaration one function, many behaviours create functions by passing default arguments to a function template set default arguments then forget about them! sub make_stove { my $heat = shift; return sub { print shift,"is cooked on",$heat,"heat" }; } my $small_stove = make_stove("low"); my $large_stove = make_stove("high"); $small_stove->("milk"); $large_stove->("water"); milk is cooked on low heat water is cooked on high heat 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References A Template Factory Let’s make a hash of functions What’s in %stoves? Sometimes data structures can get the best of us modules exist to pretty-print data structures my @stove_types = qw(tiny small medium large nuclear); my %stoves; @stoves{@stove_types} = map { make_stove($_) } @stove_types; $stoves{tiny}->(“socks"); $stoves{nuclear}->("bacon"); sock is cooked on tiny heat bacon is cooked on nuclear heat 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Data::Dumper – debugger’s friend One of the many very useful modules I’ll focus on modules at another time for now, just think of them as libraries of functions which you can call on Output of Data::Dumper can be passed to eval { } man Data::Dumper to read documentation use Data::Dumper; my $complex = {one=>1,two=>{a=>"amiable",b=>"burdensome",c=>[qw(coy cute)]}}; print Dumper($complex); $VAR1 = { 'two' => { 'a' => 'amiable', 'b' => 'burdensome', 'c' => [ 'coy', 'cute' ] }, 'one' => 1 }; 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Looking into %stove What’s in the stove? $VAR1 = { 'medium' => sub { "DUMMY" }, 'small' => sub { "DUMMY" }, 'nuclear' => sub { "DUMMY" }, 'large' => sub { "DUMMY" }, 'tiny' => sub { "DUMMY" } }; use Data::Dumper; print Dumper(\%stove) 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Persistence How do you store data between invocations of a script? text file XML database serialized data structure Sometimes it takes a long time to arrange data in memory just the way you like How do you debug step B without repeating the lengthy step A? do something with complex data structure (sort, calculate, etc) 10 min parse/organize in complex data structure 10 sec input data (e.g. text) A B 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Storable - store/retrieve The Storable module writes a data structure from memory onto disk – directly create and store my $complex = {one=>1,two=>{a=>"amiable",b=>"burdensome",c=>[qw(coy cute)]}}; use Storable; store($complex,"complex.cache"); » ls total 16 -rwxr-xr-x 1 martink users 2530 2004-02-11 23:00 coderef -rw-r--r-- 1 martink users 95 2004-02-11 23:16 complex.cache -rwxr-xr-x 1 martink users 587 2004-02-11 23:16 getcomplex -rwxr-xr-x 1 martink users 615 2004-02-11 23:16 makecomplex retrieve and use $VAR1 = { 'two' => { 'a' => 'amiable', 'b' => 'burdensome', 'c' => [ 'coy', 'cute' ] }, 'one' => 1 }; use Storable; use Data::Dumper; my $complex = retrieve("complex.cache"); print Dumper($complex); 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Storable - freeze/thaw In addition to writing serializing data to disk, you can serialize it to a list or string Use freeze/thaw to include data structures in URLs encrypt first! my $complex = {one=>1,two=>{a=>"amiable",b=>"burdensome",c=>[qw(coy cute)]}}; use Storable; my $datacan = freeze($complex); my @datacan_encoded = unpack(“C*”,$datacan); print @datacan_encoded; 4 4 4 49 50 51 52 4 4 4 8 3 2 0 0 0 4 3 3 0 0 0 10 7 97 109 105 97 98 108 101 1 0 0 0 97 10 10 98 117 114 100 101 110 115 111 109 101 1 0 0 0 98 4 2 2 0 0 0 10 3 99 111 121 10 4 99 117 116 101 1 0 0 0 99 3 0 0 0 116 119 111 8 129 3 0 0 0 111 110 101 my $datacan_reconstitute = pack("C*",@datacan_encoded); print Dumper(thaw($datacan_reconstitute)); $VAR1 = { 'two' => { 'a' => 'amiable', 'b' => 'burdensome', 'c' => [ 'coy', 'cute' ] }, 'one' => 1 }; 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Cache::Cache If you would like to implement an expiring data cache using Storable – don’t use Cache::FileCache Using this module you can associate complex data structures with expiring cache keys 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Symbolic References Dangerous, but we’ll cover them Think of these as anologous to symbolic links on a UNIX file system point to names, not inodes change name of referent and symbolic link breaks in contrast, the references we have been talking about are hard references Using a string or a number as a reference produces a symbolic reference my $fruit = “banana”; $$fruit = “yellow”; # sets $banana = “yellow”; ${$fruit} = "rotting"; # sets $banana = “rotting” ${$fruit . "rama"} = "not a fruit"; # sets $bananarama = “not a fruit” sub bananajuice { print "very sticky"; } &{$fruit . "juice"}; # calls function bananajuice 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

strict VS no strict ‘refs’ you should always start all your script using the strict pragma if you want to use symbolic references, you must include why are symbolic references so evil? use strict; use strict; no strict ‘refs’; 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Say No To Symbolic References references spring into existence when used without initialization what would have happened if $x was already defined? In other words, you expect to use a variable as a reference and it conspires against you to set the value of a named variable use strict; my $x; # some scalar @{$x} = (1,2,3); # $x is now an array reference use strict; no strict ‘refs’; my $x; # some scalar $x = “numbers”; @{$x} = (1,2,3); # @numbers is now a list 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

Slices with References References and slices slice notation my $hash; $hash->{one} = 1; $hash->{two} = 2; $hash->{three} = 3; my $hash; @$hash{qw(one two three)} = (1,2,3) my $array; $array->[0] = “one”; $array->[1] = “two”; $array->[2] = “three”; my $array; @$array[0,1,2] = qw(one two three); 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Teaser What do these refer to? $h is a hash ref $h is a hash and $h{KEY} holds scalar reference e.g. $h{KEY} = \42 $h is a hash ref and $h->{KEY} holds a scalar reference e.g. $h->{KEY} = \42 $$h{KEY} ${$h}{KEY} $h->{KEY} ${$h{KEY}} ${$h->{KEY}} 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References Common Errors You are likely to see something like this unless you are using symbolic references You are trying to dereference a variable that is NOT a reference, but a simple scalar Can't use string ("555") as a SCALAR ref while "strict refs" in use at ./recipies line 40. or Can't use string ("555") as an ARRAY ref while "strict refs" in use at ./recipies line 40. my $x = “hello”; print @$x; # ERROR but you don’t get an error if you have no strict ‘refs’ – avoid symbolic references 6/16/2018 1.1.2.8.2 - Intermediate Perl - References

1.1.2.8.2 - Intermediate Perl - References 1.1.2.8.1 Introduction to Perl – Session 1 train eyes to distinguish {} and [] {} is for HASHES [] is for ARRAYS mnemonic: CURLY HASHES SQUARE ARRAYS 6/16/2018 1.1.2.8.2 - Intermediate Perl - References