The distinctive Perl camel is (c) O'Reilly
Perl Workshop Home Page
Home of the Bioinformatics Perl Workshop perl workshop > courses > introduction to perl (1.0.1.8) > Subroutines and Special Variables (.6/8) > sub-01 (.c5)

course 1.0.1.8

Level: beginner
1.0.1.8.6
$_; other special variables; introduction to subroutines; passing arguments; @_; named arguments; call context

legend

course code

cat.course.level.sessions.session

e.g. 1.0.1.8

categories

0 | introduction and orientation

1 | perl fundamentals

2 | shell and prompt tools

3 | web development

4 | CPAN Modules

5 | Ruby

levels

level: all all ( 0 )

level: beginner beginner ( 1 )

level: intermediate intermediate ( 2 )

level: advanced advanced ( 3 )

[ have you wondered how many different banners there are? ]

lecture code viewer

downloads

Code
Subroutines and Special Variables
Subroutines and Special Variables
Martin Krzywinski
#!/usr/local/bin/perl $\ = "\n"; $, = " "; print sum(1); print sum(1,2,3); print square(5,5); # compute and return the sum of a list sub sum { $sum = 0; # iterating through subroutine arguments for (@_) { # $_ alias to each argument $sum += $_; } return $sum; } sub square { $num = @_; return $num**2; } sub makesequence { %args = @_; } $b= 0 if ! defined $b; print make_sequence(len=>10,bp=>"at"); print make_sequence(bp=>"gcn",len=>0); # create a random n-mer from a specified vocabulary sub make_sequence { %args = @_; @bp = split("",$args{bp}); $seq = ""; for (1..$args{len}) { $seq .= $bp[rand(@bp)]; } return $seq; } print urds(5); sub urds { my ($n) = @_; @urds = (); for (1..$n) { push @urds, rand; } return @urds; } $seq = "atgctggg"; ($x) = filter_seq($seq,"gc"); print $x; sub filter_seq { ($seq,$testbp) = @_; @passedseq = (); for (split("",$seq)) { push @passedseq, $_ if /[$testbp]/; $_ = 1; } return scalar @passedseq; }

6 | Subroutines and Special Variables | 1.0.1.8.6

1.0.1.8.6.c1 | loop-01 | Martin Krzywinski | code
1.0.1.8.6.c2 | loop-02 | Martin Krzywinski | code
1.0.1.8.6.c3 | loop-03 | Martin Krzywinski | code
1.0.1.8.6.c4 | loop-04 | Martin Krzywinski | code
1.0.1.8.6.c5 | sub-01 | Martin Krzywinski | code
1.0.1.8.6.c6 | sub-02 | Martin Krzywinski | code
1.0.1.8.6.a1 | Subroutines and Special Variables | Martin Krzywinski | pdf
1.0.1.8.6.p1 | Subroutines and Special Variables | Martin Krzywinski | ppt
1.0.1.8.6.s1 | Subroutines and Special Variables | Martin Krzywinski | slides