Lec10Handouts

Lec10Handouts - CS2044 Advanced Unix Tools Spring 2009...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2044 - Advanced Unix Tools Spring 2009 Lecture 10 David Slater dms236 at cornell.edu March 18, 2009 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Organization Homework 3 Due Sunday March 15th Start Early! Turn In Code That Works! Follow Directions David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 2
Subroutines In Perl, functions are called subroutines and are written sub mysubroutine { some code; some more code; best code ever; } Like everything else in Perl white space does not matter. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Calling Subroutines You can declare a scalar variable $mysubroutine and it will be separate Subroutines go anywhere in your script A common convention is to put them all at the bottom or the top. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 4
More Subroutine Stuff A Subroutine will return the value of the last expression automatically, but be careful as to what the last expression is! #!/usr/bin/perl $total = &get_total; print("The total is $total\n"); sub get_total { $value = 0; chomp($inputline = <STDIN>); @subnumbers = split(/\s+/, $inputline); $number = 0; while ($subnumbers[$number] ne "") { $value += $subnumbers[$number++]; } } What’s wrong with this? David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What was wrong The last expression evaluated was $subnumbers[$number] ne "" This value is zero (the last time it runs), and since zero is treated as the null string by print in Perl, the script prints "The total is" . Fix: #!/usr/bin/perl $total = &get_total; print("The total is $total\n"); sub get_total { $value = 0; chomp($inputline = <STDIN>); @subnumbers = split(/\s+/, $inputline); $number = 0; while ($subnumbers[$number] ne "") { $value += $subnumbers[$number++]; } $retval=$value; } David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 6
RETURN!! Of course, we could have just used the Perl return statement return(some value) instead. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Passing values to subroutines If you call a subroutine using , then value1 value2 and value3 are stored in the array @_ David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Background image of page 8
Thats about all the Perl we have time to cover, but there is alot more you can do with it Object Oriented Programming Extend it with Modules Work with a ton of System Functions And much much more But with that we turn our attention to. .. David Slater dms236 at cornell.edu
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

Lec10Handouts - CS2044 Advanced Unix Tools Spring 2009...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online