• Save
Python & Perl: Lecture 26
Upcoming SlideShare
Loading in...5
×
 

Python & Perl: Lecture 26

on

  • 707 views

 

Statistics

Views

Total Views
707
Views on SlideShare
533
Embed Views
174

Actions

Likes
0
Downloads
0
Comments
0

7 Embeds 174

http://vkedco.blogspot.com 135
http://www.vkedco.blogspot.com 28
http://vkedco.blogspot.in 5
http://vkedco.blogspot.de 3
http://www.vkedco.blogspot.in 1
http://vkedco.blogspot.fr 1
http://vkedco.blogspot.com.br 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Python & Perl: Lecture 26 Python & Perl: Lecture 26 Presentation Transcript

    • Python & Perl Lecture 26 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Subroutines – Definition Syntax – Argument Lists – Argument Value Modification – Recursion – Lexical vs Dynamic Scope in Subroutines – Named Parameterswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Subroutineswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Subroutine Definition Syntax● Every subroutine (function) starts with the keyword sub followed by a valid Perl identifier that names the subroutine● There is no argument list after the subroutines name (more on argument lists later in this lecture)● The subroutines body (aka code block) is enclosed in {} www.vkedco.blogspot.com
    • Subroutine Definition Syntax sub subroutine_name { statement_0; statement_1; statement_2; ... statement_n; }www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example subs_with_no_args.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Argument Lists ● What do you do if you need to pass arguments to a subroutine? ● Perl has a special variable, @_, which stores the list of arguments passed to a subroutines ● @_ is an array and can be manipulated in the subroutines body to get argument valueswww.youtube.com/vkedco
    • Example subs_with_args.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Special Variable @_ ● What do you do if you need to pass arguments to a subroutine? ● Perl has a special variable, @_, which stores the list of arguments passed to a subroutines ● @_ is an array and can be manipulated in the subroutines body to get argument values ● Hashes are flattened into lists when stored in @_www.youtube.com/vkedco www.vkedco.blogspot.com
    • Special Variable @_ ● There are two methods to get argument values from @_ ● Method 1: Assign values to local variables from @_ Example: my ($x, $y) = @_; ● Method 2: Use shift() to pop values from @_ into local variables Example: my $x = shift(); my $y = shift();www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example sub_args_manip.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Modifying Argument Values ● Scalars, arrays, and hashes are passed by reference to subroutines ● The values of arguments passed by reference can be directly modified by the subroutines ● Safe subroutines must make local copies of arguments to prevent their values from being modifiedwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example pass_by_ref.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Recursive Subroutines ● Recursive subroutines are defined in Perl in much the same way as they are defined in other programming languages ● Recursive subroutines are always elegant but not always efficient ● If you can solve the problem iteratively, go with the iterative solutionwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example recursive_subs.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Lexical vs. Dynamic in Subroutines ● Lexical and dynamic variables exist in the scopes where they are defined ● Dynamic variables, unlike lexical ones, are seen to all subroutines called in the scope where they are defined ● Caveat: this coding style may lead to programs that are hard to debugwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example lexical_vs_dynamic.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Named Parameters ● Named parameters are the same as keyword parameters in Python ● They should be used in subroutines with lots of arguments some of which may be optional ● Named parameters are turned into a hash from @_ and then manipulated as a hash to access their valueswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example named_parameters.plwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Reading & References ● http://perldoc.perl.org/ ● James Lee. Beginning Perl, 2 nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hallwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Feedback Bugs, comments to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com