Your SlideShare is downloading. ×
Python & Perl: Lecture 24
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Python & Perl: Lecture 24

705
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
705
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python & Perl Lecture 24 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Identifiers & Scopes ● Array Manipulation – Assigning from arrays – Computing array lengths – Iterating through arrays ● EXIT & DIEwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. Identifiers & Scopeswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. Scopes ● An identifiers scope is the portion of the program where the identifier can be referenced ● Some identifiers can be referenced from anywhere in the program ● Other identifiers can be referenced from specific sections of the program ● In Perl, an identifier can have three scopes: global, lexical, and dynamicwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Global Scope ● The keyword our defines a global variable ● If there is no keyword in front of a variable, it becomes global by default ● Global variables exist for the entire execution of the program and can be manipulated from anywhere in the program ● Examples: our $x = 10; our @ary = (1 .. 5);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. Lexical Scope ● The keyword my defines a lexical identifier ● A lexically scoped identifier exists only during the block in which it is defined ● Examples: my $x = 10; my @ary = (1 .. 5);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Dynamic Scope ● The keyword local defines a dynamic identifier ● Like a lexically scoped identifier, a dynamically scoped identifier exists in the block in which it is created ● In addition, dynamic identifiers are accessible in to all subroutines (Perl term for functions) called from that block in which they (identifiers) are defined ● Examples: local $x = 10; local @ary = (1 .. 5);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. Strict Variable Scoping ● Place “use strict;” at the beginning of your Perl file to make sure that all identifiers are explicitly scoped ● Perl uses packages (Perls term for namespaces) to determine the accessibility of identifiers ● Note that “use strict;” will cause some code not to compilewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. Example: scoping_01.pl #!/usr/bin/perl use warnings; $str = rock violin; ## there is no “use strict;”, the compiler is silent print We like . $str . "n"; { ## entering a new scope $str = classical violin; ## $str is set to classical violin print We like . $str . "n"; } ## leaving the scope ## global $str is still classical violin print We like . $str . "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. Example: scoping_01.pl ● The output of scoping_01.pl is as follows: We like rock violin We like classical violin We like classical violinwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. Example: scoping_02.pl #!/usr/bin/perl use warnings; use strict; ## with this in place, the program does not compile. $str = rock violin; print We like . $str . "n"; { ## entering a new scope $str = classical violin; ## $str is set to classical violin print We like . $str . "n"; } ## leaving the scope ## global $str is still classical violin print We like . $str . "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Example: scoping_02.pl ● The output of scoping_02.pl is as follows: Global symbol "$str" requires explicit package name at ./scoping_02.pl line 9. Global symbol "$str" requires explicit package name at ./scoping_02.pl line 11. Global symbol "$str" requires explicit package name at ./scoping_02.pl line 13. Global symbol "$str" requires explicit package name at ./scoping_02.pl line 15. Global symbol "$str" requires explicit package name at ./scoping_02.pl line 18. Execution of ./scoping_02.pl aborted due to compilation errors.www.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. Example: scoping_03.pl #!/usr/bin/perl use warnings; use strict; our $str = rock violin; ## we add our and the program compiles. print We like . $str . "n"; { ## entering a new scope $str = classical violin; ## $str is set to classical violin print We like . $str . "n"; } ## leaving the scope ## global $str is still classical violin print We like . $str . "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. Example: scoping_03.pl ● The output of scoping_03.pl is as follows: We like rock violin We like classical violin We like classical violinwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. Example: scoping_04.pl #!/usr/bin/perl use warnings; use strict; our $str = rock violin; ## $str is global print We like . $str . "n"; { ## entering a new scope my $str = classical violin; ## this $str is lexical print We like . $str . "n"; } ## leaving the scope ## global $str is still rock violin print We like . $str . "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. Example: scoping_04.pl ● The output of scoping_04.pl is as follows: We like rock violin We like classical violin We like rock violinwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. Array Manipulationwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. Three Cases of Array Assignment ● Case 1: Array has as many values as there are variables ● Case 2: Array has fewer values than there are variables ● Case 3: Array has more values than there are variableswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. Example array_assignment.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. Computing Array Length ● Perls function length returns the number of characters in its string argument ● So it can be used to compute the length of strings in characters ● This function cannot be used compute the length of an array, because it returns the number of characters in the integer that denotes the length of the array argumentwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. Example length.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. Computing Array Length ● If @data is an array, $#data refers to its last index ● If @data is (1, 2, 3), then $#data == 2 ● If @data is an array, the length of @data is $#data + 1 ● Another way to compute the length of @data is scalar @datawww.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. Example array_length.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. Iterating Through Arrays ● You can use while loops to iterate through arrays ● You can also use for and foreach loops to iterate through arrays ● If there is no control variable used, the array values are assigned to $_www.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. Examples array_foreach.pl foreach_expression.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. EXIT & DIEwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 27. EXIT ● As its name suggests, exit() exits a Perl program and returns the value back to the calling program or the OS ● The value of 0 means that the program terminated normally ● A non-zero value, typically 1, signals an abnormal termination ● If no argument is provided, 0 is returnedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 28. DIE ● The function die() is used for serious errors ● die() takes a string and prints it to the standard error output ● die() calls exit() with a non-zero value ● die() is a great debugging tool: it prints out the line number of the program where it was executedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 29. 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
  • 30. Feedback Bugs, comments to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com