Introduction to Perl Session 6. special variables subroutines Introduction to Perl

Similar documents
Modularity and Reusability I. Functions and code reuse

Lecture 2: Programming in Perl: Introduction 1

Perl Library Functions

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

CSCI-GA Scripting Languages

Classnote for COMS6100

A control expression must evaluate to a value that can be interpreted as true or false.

Subroutines in Perl. Jon-Michael Deldin. Dept. of Computer Science University of Montana September 12, 2011

Control Structures. Important Semantic Difference

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

More Perl. CS174 Chris Pollett Oct 25, 2006.

The Dynamic Typing Interlude

(Refer Slide Time: 01:12)

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

Intermediate Perl Session Intermediate Perl. map transforming data. sort ranking data. grep extracting data

CS 230 Programming Languages

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Intro. Scheme Basics. scm> 5 5. scm>

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

Fortunately, the layout is much more legible, more like BASIC's PRINT USING statement. Think of it as a poor man's nroff(1). nroff

Welcome to Research Computing Services training week! November 14-17, 2011

Pathologically Eclectic Rubbish Lister

CS 105 Perl: Completing the Toolbox

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

PERL Scripting - Course Contents

1. Introduction. 2. Scalar Data

What is PERL?

CSCC24 Functional Programming Scheme Part 2

Ruby: Introduction, Basics

Computer Programming

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

IT441. Network Services Administration. Perl: File Handles

# use a BEGIN block so we print our plan before MyModule is loaded BEGIN { plan tests => 14, todo => [3,4] }

COMS 3101 Programming Languages: Perl. Lecture 2

Documentation Nick Parlante, 1996.Free for non-commerical use.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

COMS 3101 Programming Languages: Perl. Lecture 5

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Introduction to Scheme

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

CS 105 Perl: Perl subroutines and Disciplined Perl

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

Intro to Haskell Notes: Part 5

Full file at C How to Program, 6/e Multiple Choice Test Bank

CS115 - Module 3 - Booleans, Conditionals, and Symbols

Repetition Structures II

@EXPORT_OK = qw(munge frobnicate); # symbols to export on request

Arrays (Lists) # or, = ("first string", "2nd string", 123);

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

CS 5142 Scripting Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

use Params::Check qw[check allow last_error];

Fundamentals of Programming Session 23

Chapter 2 Basic Elements of C++

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

GOLD Language Reference Manual

Examples of Using the ARGV Array. Loop Control Operators. Next Operator. Last Operator. Perl has three loop control operators.

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

2/6/2018. ECE 220: Computer Systems & Programming. Function Signature Needed to Call Function. Signature Include Name and Types for Inputs and Outputs

IT441. Subroutines. (a.k.a., Functions, Methods, etc.) DRAFT. Network Services Administration

package YourModule; require = = qw(munge frobnicate); # symbols to export on request

BIOS 546 Midterm March 26, Write the line of code that all Perl programs on biolinx must start with so they can be executed.

DaMPL. Language Reference Manual. Henrique Grando

CSE 374 Programming Concepts & Tools

pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap

CSCI 171 Chapter Outlines

Object Oriented Programming and Perl

Lab copy. Do not remove! Mathematics 152 Spring 1999 Notes on the course calculator. 1. The calculator VC. The web page

UNIT V Sub u P b ro r g o r g a r m a s

Functional Programming. Big Picture. Design of Programming Languages

my $full_path = can_run('wget') or warn 'wget is not installed!';

About this exam review

COMS 3101 Programming Languages: Perl. Lecture 1

Bioinformatics. Computational Methods II: Sequence Analysis with Perl. George Bell WIBR Biocomputing Group

Software Design and Analysis for Engineers

Lecture 1. Torgeir R. Hvidsten Assistant professor in Bioinformatics Umeå Plant Science Center (UPSC) Computational Life Science Centre (CLiC)

Fundamentals of Programming Session 25

Chapter 9 :: Subroutines and Control Abstraction

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CSE 341, Autumn 2015, Ruby Introduction Summary

my $reply = $term->get_reply( prompt => 'What is your favourite colour?', choices => [qw blue red green ], default => blue, );

COMP-202 Unit 4: Programming with Iterations

Programming Style Guide v1.1

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

So what does studying PL buy me?

UNIX Shell Programming

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

Chapter 13 Control Structures

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

my $full_path = can_run('wget') or warn 'wget is not installed!';

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Transcription:

1.0.1.8.6 Introduction to Perl Session 6 special variables subroutines 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 1

I/O Recap file handles are created using open(f,$file); for reading $file for writing >$file for appending >>$file records are read from the filehandle using <F> open(f,$file); while($line = <F>) { chomp $line; ($a,$b,$c) = split(,$line); close(f); open(f, >$file ); for $line (@lines) { @tokens = split(, $line); printf F %d %s\n, @tokens; close(f); 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 2

Special Variables Perl has a large number of special variables special variables are contextual store helpful values special variables can radically change the behaviour of your code special variables are used as default inputs to certain functions special variable names are generally unusual and the names do not adhere to naming rules of variables you can create $_ $, $\ $1 special variables help you write more concise code 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 3

Special Variables - $a $b we have already seen special variables $a and $b magic sauce in sort code do not need to be declared ahead of time take on different values as code runs sort { $b <=> $a @nums; another special variable we saw was $#array stored last index value of @array could be used to explicitly shrink the array 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 4

Special Variables - $_ the variable $_ is ubiquitous in Perl code, even when it is not explicitly mentioned it is the default input to many functions it holds the value in the current input, iteration or pattern search space for $num (@nums) { print $num; for $_ (@nums) { print $_; for (@nums) { print; within a for loop, $_ points to the current value in the list $_ is not a copy of the variable it is an alias assigning a value to $_ changes the value in the list without arguments, print will send the value of $_ to standard output 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 5

$_ in loops for (1..5) { # $_ holds the value 1, 2, 3, etc as the loop iterates $x = rand(); printf( number %d is %f,$_,$x); number 1 is 0.945200 number 2 is 0.586325... $_ points to the current iterator value of the immediate loop 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 6

$_ in nested loops $_ is the iterator value of inner-most loop in which it appears for (1..3) { # printing value of $_ in (1..3) loop print; for (a..c) { # printing value of $_ in (a..c) loop print; 1 a b c 2 a b c 3 a b c 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 7

$_ as default argument we ve seen that print without an argument prints $_ unary operators like defined also test $_ if no arguments are passed for (1..2,undef) { print if defined; for (a,undef,b) { print; 1 a b 2 a b a b undef from (a,undef,b) undef from (a,undef,b) undef from (a,undef,b) 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 8

$_ as default argument chomp and split and m// also take $_ as default argument @bp = qw(a t g c); for (1..5) { @seq = (); for (1..10) { push @seq, $bp[rand(@bp)]; # create a random string a t g c... a t\n push @lines, join(" ",@seq)."\n"; for (@lines) { # print $_ print; # remove trailing newline from $_ chomp; # skip if $_ does not match /a a a/ next if! /a a a/; # split $_ along whitespace @bp = split; print join( :,@bp); g g a a a t t a c a g:g:a:a:a:t:t:a:c:a a a g a a c a g t g c a c t t c t t c c c g g c g c t a a a c:g:g:c:g:c:t:a:a:a t a a a t t c t a a t:a:a:a:t:t:c:t:a:a 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 9

$_ for conciseness $_ helps you limit verbosity in your code calling functions without arguments may feel strage, but the feeling will pass for $line (@lines) { print $line; chomp $line; next if! $line =~ /a a a/; @bp = split(,$line); for $_ (@lines) { print $_; chomp $_; next if! $_ =~ /a a a/; @bp = split(,$_); for (@lines) { print; chomp; next if! /a a a/; @bp = split; you should rarely need to explicitly refer to $_ in your code next if /abc/ instead of next if $_ =~ /abc/ print instead of print $_ chomp instead of chomp $_ except in cases where no default argument is possible printf( %s %d,$string,$_); $sum += $_; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 10

capital crimes with $_ $_ is an alias not a copy you should never assign values to $_ under penalty of becoming a donkey @nums = (1..3); for (@nums) { print; # insidious $_ = 6; for (@nums) { print; 1 2 3 think of $_ as a pointer to the current iterated value if you change $_, you change the value for this reason, $_ is used as a read-only variable in vast majority of cases if you need to work with the value of $_ destructively, assign it to a variable $line = $_ 6 6 6 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 11

obfuscation with $_ you can use the alias nature of $_ to alter a list I strongly recommend you never do this @nums = (1..3); for (@nums) { $_++; # nums now (2,3,4); for (@nums) { $_ = sprintf( %d.%d,$_**2,$_); # nums now (4.2,9.3,16.4); for (@nums) { $_ = int; # nums now (4,9,16); 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 12

Other Special Variables assignment man perlvar read about the following special variables $_ @_ $, $ $\ $/ $$ $. write a script that uses all these special variables 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 13

Introduction to Subroutines a subroutine is a named chunk of code you can call as a function adds modularity to your scripts helps you reuse code @bp = qw(a t g c); @lines = (); for (1..5) { @seq = (); for (1..10) { push @seq, $bp[rand(@bp)]; # create a random string a t g c... a t push @lines, join(" ",@seq); @lines = (); for (1..5) { # call the function, store output in $seq $seq = make_sequence(); push @lines, $seq; # create a random string a t g c... a t sub make_sequence { @bp = qw(a t g c); @seq = (); for (1..10) { push @seq, $bp[rand(@bp)]; $seq = join(,@seq); return $seq; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 14

Introduction to Subroutines you provide the name of the subroutine make the name explicit and specific get_gc_ratio() vs process_sequence() remove_vowels() vs munge_string() variety of naming conventions exist getstringlength() get_string_length() e.g., imperative verb + (adjective) + noun get_next_record() store_current_state() subroutines generally return values via return always call subroutines with (), even if no arguments are passed $x = NAME(); sub NAME {... return $value; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 15

Passing Arguments subroutines are most useful when they accept arguments that control their behaviour consider the subroutine below which creates a random 10mer what about making an n-mer? $seq = make_sequence(); # create a random 10-mer # this is not a very reusable function sub make_sequence { @bp = qw(a t g c); $seq = ; for (1..10) { $seq.= $bp[rand(@bp)]; return $seq; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 16

Passing Arguments a subroutine accepts a list as argument (one or more scalars) the special variable @_ within the subroutine is populated with aliases to the arguments elements of @_ are $_[0], $_[1], $_[2],... just like $_, do not modify @_ modifying @_ changes the values of the original variables mysub(1,2,3); sub mysub { # arguments available via @_ special variable # assign to variables in one shot ($arg1,$arg2,$arg3) = @_; # or separately $arg1 = $_[0]; $arg2 = $_[1]; $arg3 = $_[3]; return $arg1+$arg2+$arg3; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 17

Passing Arguments upon receiving @_ in the function, it is customary to create a copy of the values to prevent inadvertent modification print sum(1); print sum(1,2,3); # compute and return the sum of a list sub sum { # explicitly make a copy of arguments @nums = @_; $sum = 0; for (@nums) { $sum += $_; return $sum; sub sum { $sum = 0; # iterating through @_ directly for (@_) { # $_ alias to each argument $sum += $_; return $sum; in certain cases, if you re careful, you can traverse @_ directly make sure what you are doing is going to be obvious to the reader in other cases, copying @_ is too costly and you need to work with aliases 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 18

Passing Arguments it is customary to create specifically named variables to each argument to create self-documenting code $seq = make_sequence(50); # create a random $len-mer sub make_sequence { # create argument variable $len = $_[0]; @bp = qw(a t g c); $seq = ; for (1..$len) { $seq.= $bp[rand(@bp)]; return $seq; $seq = make_sequence(50); # create a random $_[0]-mer sub make_sequence { @bp = qw(a t g c); $seq = ; # access @_ directly for (1..$_[0]) { $seq.= $bp[rand(@bp)]; return $seq; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 19

Challenge what does square(5) return? print square(5); # there is a bug here sub sum { my $num = @_; return $num**2; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 20

Named Arguments Perl does not natively support named arguments arguments passed as a list arrive in the same order and you need to remember the order when calling the function recall that a hash is a 2n-element list pass in a hash with keys as variable names %hash = (len=>50,bp=> atg ); $seq = make_sequence(%hash); $seq = make_sequence(len=>10, bp=> at ); $seq = make_sequence(bp=> gcn, len=>5); # create a random n-mer from a specified vocabulary sub make_sequence { # we are coercing an array to be stored as a hash # will break if @_ has odd number of elements %args = @_; @bp = split(,$args{bp); $seq = ; for (1..$args{len) { $seq.= $bp[rand(@bp)]; return $seq; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 21

Checking Argument Integrity it s very wise to check the integrity of arguments before using them recall the difference between if $x and if defined $x # create a random n-mer from a specified vocabulary sub make_sequence { # we are coercing an array to be stored as a hash # will break if @_ has odd number of elements %args = @_; if(! length($args{bp)) { print empty vocabulary string ; return undef; if(! defined $args{len $args{len < 0) { print undefined or negative sequence length ; return undef @bp = split(,$args{bp); $seq = ; for (1..$args{len) { $seq.= $bp[rand(@bp)]; return $seq; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 22

Default Arguments if arguments fail checks, it is customary to assign default values sub some_function { ($a,$b) = @_; # sets $a=10 if $a is false (i.e. 0 is considered unacceptable) $a = 10; # sets $b=10 if $b is not defined (i.e. 0 is considered acceptable) $b = 10 if! defined $b;... = operator is helpful here $a = 5 $a = $a 5 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 23

Returning Different Kinds of Variables subroutines may return any kind of variable the caller must be aware of the behaviour of the subroutine $x = sub1(); sub sub1 {... return $x; # returns a scalar @y = sub2(); sub sub2 {... push @y, 10;... return @y; # returns an array %z = sub3(); sub sub3 {... $z{red = apple ;... return %z; # returns a hash 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 24

return Context this is a tricky point, but extremely important sit comfortably recall that context is very important when cross-assigning variables $scalar = @array has special meaning consider a function that returns N random numbers # return N uniform random deviates sub urds { my ($n) = @_; @urds = (); for (1..$n) { push @urds, rand; return @urds; 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 25

return Context now look at how urds(3) behaves in these two situations print urds(3); 0.329912258777767 0.549033692572266 0.577604257967323 print 1+urds(3); 4 in the first case, print takes a list as its argument and therefore urds(3) is called in array context in the second case, + takes two scalars as arguments thus urds(3) is called in scalar context 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 26

return Context consider this function which returns a list of filtered base pairs given $seq, return a list of base pairs in this string that are one of the characters in $testbp # return base pairs from $seq that match $testbp sub filter_seq { ($seq,$testbp) = @_; @passedseq = (); for $bp (split(,$seq)) { # pass $bp if it is matched by character chass [$testbp] # i.e. if it matches one of the characters in $testbp push @passedseq, $bp if /[$testbp]/; return @passedseq; print filter_seq( aaatttgggccc, ag ); # (a a a g g g) $num_filtered = filter_seq( aaatttgggccc, ag ); # 6 # ($x) = @array idiom for getting the first element out of the array ($num_filtered) = filter_seq( aaatttgggccc, ag ); # a 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 27

return Context do not assume how your function will be used if you mean to return a scalar value and there is possibility of it being evaluated in array context and returning a list return scalar # return base pairs from $seq that match $testbp sub filter_seq { ($seq,$testbp) = @_; @passedseq = (); for $bp (split(,$seq)) { push @passedseq, $bp if /[$testbp]/; return scalar @passedseq; print filter_seq( aaatttgggccc, ag ); # 6 $num_filtered = filter_seq( aaatttgggccc, ag ); # 6 ($num_filtered) = filter_seq( aaatttgggccc, ag ); # 6 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 28

Returning Failure you may wish to return failure to indicate that something has gone wrong in light of the previous slides, you should be hearing a warning klaxon how do you ensure failure in multiple contexts? we know enough not to return 0, so how about return undef sub simulate_failure{ return undef; $x = simulate_failure(); print failure scalar x if! defined $x; @x = simulate_failure(); print failure array x if! defined @x; failure scalar x oops! 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 29

Returning Failure why did defined @x return true? sub simulate_failure{ return undef; @x = simulate_failure(); # @x is now (undef), a list with a single undef element # this list evaluates to true a bare return will always return strong failure (fails defined test) in the appropriate context sub simulate_failure{ return; @x = simulate_failure(); # @x is now truly undefined, a list that fails defined check 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 30

1.0.8.1.6 Introduction to Perl Session 6 you now know $_ and @_ subroutines more about context next time more on string manipulation replacement and transliteration operators global searches contextual behaviour of =~ 6/17/2008 1.0.1.8.6 - Introduction to Perl - Special Variables and Subroutines 31