Python & Perl          Perl Modules & Packages                     Vladimir Kulyukinwww.youtube.com/vkedco           www.v...
Outline   ●   Review of Python Packages & Modules   ●   Perl Packages   ●   Perl Modules   ●   Perl Exporter Packageswww.y...
Review           Python Modules & Packageswww.youtube.com/vkedco       www.vkedco.blogspot.com
Modules   ●   A PY module is a .py file   ●   A module may contain any syntactically       valid Python code   ●   Some PY...
Module Import Patterns      ●   import module      ●   import module1, module2, …, moduleN      ●   import module as anoth...
Telling Python Where to Look   ●   Put your modules into one of the paths in       sys.path   ●   To find out the current ...
What Python Loads   ●   When Python needs a modules byte code, it generates it       automatically   ●   Python looks for ...
Python Packageswww.youtube.com/vkedco      www.vkedco.blogspot.com
What Makes a Python Package   ●   A package is a directory that contains a       set of modules (set can be empty)   ●   W...
MY_MATH Applicationwww.youtube.com/vkedco   www.vkedco.blogspot.com
MY_Math Application Source Code                         source is herewww.youtube.com/vkedco           www.vkedco.blogspot...
Perl Packages & Moduleswww.youtube.com/vkedco    www.vkedco.blogspot.com
PL Modules   ●   A PL package is a namespace   ●   A PL module is a package within a file   ●   Large collection of PL mod...
Module Use   ●   Modules are used with the use pragma   ●   When Perl sees the use pragam anywhere in       a program, it ...
Modifying @INC & Defining LIB Path        unshift @INC, “/home/python/code/”;        use lib “/home/python/code/”;www.yout...
Steps to Create a PL Module   ●   Come up with a meaningful name (e.g.,       MyMathPL)   ●   Put your source code in MyMa...
Putting 1; at the End of a Perl Module   ●   Why you should put 1; at the end of a Perl       module   ●   Earlier version...
Three Ways to Import Perl Modules   ●   DO “FILE_NAME.PL”;   ●   REQUIRE MODULE_NAME;   ●   USE MODULE_NAME;www.youtube.co...
DO FILE_NAME   ●   DO pragma looks at @INC and, if it cannot find       the file, does nothing   ●   If it is found, the c...
TO_DO.PL        #!/usr/bin/perl        use warnings;        use strict;        ## our - defines a lexically scoped alias f...
DOIT.PL        #!/usr/bin/perl        use warnings;        ## This will print out.        print $global_str, "n";        #...
Output      >>> ./to_do.pl      ## $global_str is seen in to_do.pl      This is a global string      ## $lexical_str is no...
REQUIRE FILE_NAME   ●   REQUIRE pragma works like DO:          –   It looks for FILE_NAME in @INC          –   It includes...
USE FILE_NAME   ●   USE pragma works like REQUIRE:          –   It looks for FILE_NAME in @INC          –   It includes th...
DO FILE_NAME   ●   DO pragma looks at @INC and, if it cannot find       the file, does nothing   ●   If it is found, the c...
MyMathPL Application                  source code is herewww.youtube.com/vkedco       www.vkedco.blogspot.com
MyMathPL File Structure     /MyMathPL          Constants.pm          /Add            AddTwo.pm            AddThree.pm     ...
Constants.PM        package MyMathPL::Constants;        use strict;        use warnings;        sub app_name { return MyMa...
AddTwo.PM        package MyMathPL::Add::AddTwo;        ## this is the root directory where MyMathPL folder is placed. Set ...
SubtTwo.PM        package MyMathPL::Subt::SubtTwo;        ## this is the root directory where MyMathPL folder is placed. S...
MultTwo.PM        package MyMathPL::Mult::MultTwo;        ## this is the root directory where MyMathPL folder is placed. S...
Testing MyMathPL        use lib "D:/Dropbox/teaching/PythonPerl/s13/lectures/18/";        use warnings;        use strict;...
Exporter Packageswww.youtube.com/vkedco      www.vkedco.blogspot.com
Exporting Symbols   ●   If you call any functions (e.g., add2 or subt4) without       the         fully      qualified    ...
package MyMathPL::Add::AddTwoEx;                                                                                          ...
Testing MyMathPL with Exporter Modules        use lib ""; ## Set this to the folder where MyMathPL folder is located.     ...
Reading & References  ●   http://docs.python.org/py3k/tutorial/modules.html  ●   www.cpan.org  ●   http://perldoc.perl.org...
Upcoming SlideShare
Loading in …5
×

Python & Perl: Perl Modules & Packages; Perl Exporter Packages

1,337 views
1,309 views

Published on

Python & Perl: Perl Modules & Packages; Perl Exporter Packages

Published in: Science
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,337
On SlideShare
0
From Embeds
0
Number of Embeds
516
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: Perl Modules & Packages; Perl Exporter Packages

  1. 1. Python & Perl Perl Modules & Packages Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Review of Python Packages & Modules ● Perl Packages ● Perl Modules ● Perl Exporter Packageswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. Review Python Modules & Packageswww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Modules ● A PY module is a .py file ● A module may contain any syntactically valid Python code ● Some PY modules are not associated with .py files: – The sys module is not associated with any .py file – Modules written in other languageswww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Module Import Patterns ● import module ● import module1, module2, …, moduleN ● import module as another_name ● from module import name ● from module import name1, name2, …, nameN ● from module import name as another name ● from module import *www.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Telling Python Where to Look ● Put your modules into one of the paths in sys.path ● To find out the current value of sys.path: >>> import sys >>> print sys.path >>> [/home/vladimir, /usr/bin, /usr/lib/python2.7, /usr/lib/python2.7/plat-linux2, /usr/lib/python2.7/lib- tk, /usr/lib/python2.7/lib-old, /usr/lib/python2.7/lib- dynload, /usr/local/lib/python2.7/dist-packages, /usr/lib/python2.7/dist-packages]www.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. What Python Loads ● When Python needs a modules byte code, it generates it automatically ● Python looks for a file with the same name as the modules .py file that has the extension .pyo ● If there is no .pyo file or if it is older than the .py file, Python looks for a file with the extension .pyc ● If an up-to-date .pyc file is found, Python loads it ● If there is no up-to-date .pyc file, Python compiles and loads the .py filewww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Python Packageswww.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. What Makes a Python Package ● A package is a directory that contains a set of modules (set can be empty) ● What makes a directory a package is the presence of the file __init__.py ● __init__.py defines the structure of a package ● __init__.py can be emptywww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. MY_MATH Applicationwww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. MY_Math Application Source Code source is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Perl Packages & Moduleswww.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. PL Modules ● A PL package is a namespace ● A PL module is a package within a file ● Large collection of PL modules at CPAN – Data::Dumper – File::Find – Getopt::Std – Win32::TieRegistrywww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Module Use ● Modules are used with the use pragma ● When Perl sees the use pragam anywhere in a program, it includes the module specified in the pragma before doing anything else in the program ● @INC is a list of search directories Perl uses to include moduleswww.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Modifying @INC & Defining LIB Path unshift @INC, “/home/python/code/”; use lib “/home/python/code/”;www.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Steps to Create a PL Module ● Come up with a meaningful name (e.g., MyMathPL) ● Put your source code in MyMathPL.pm ● Define the package for MyMathPL.pm in the file MyMathPL.pm with the package pragma ● Remember to return 1 at the end of MyMathPL.pm!!!www.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Putting 1; at the End of a Perl Module ● Why you should put 1; at the end of a Perl module ● Earlier versions of Perl use to require that each module return a true value (1 in Perl) when used by a program ● PL modules return the last line code in a .pm file ● Newer versions of Perl do not require 1; at the endwww.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Three Ways to Import Perl Modules ● DO “FILE_NAME.PL”; ● REQUIRE MODULE_NAME; ● USE MODULE_NAME;www.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. DO FILE_NAME ● DO pragma looks at @INC and, if it cannot find the file, does nothing ● If it is found, the code from FILE_NAME is inserted into the program that executes DO as a block of code ● Lexical variables in FILE_NAME are not be seen from in the program that executes DOwww.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. TO_DO.PL #!/usr/bin/perl use warnings; use strict; ## our - defines a lexically scoped alias for a global variable. our $global_str = This is a global string; my $lex_str = This is a lexical string; do "doit.pl";www.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. DOIT.PL #!/usr/bin/perl use warnings; ## This will print out. print $global_str, "n"; ## This will not print out because lexical variables in TO_DO.PL ## that executes DO on this file. print $lexical_str, "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Output >>> ./to_do.pl ## $global_str is seen in to_do.pl This is a global string ## $lexical_str is not seen in to_do.pl Use of uninitialized value $lexical_str in print at doit.pl line 7.www.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. REQUIRE FILE_NAME ● REQUIRE pragma works like DO: – It looks for FILE_NAME in @INC – It includes the required source into the main program ● Two differences: – FILE_NAME is included only once – If FILE_NAME is not found, error is signaledwww.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. USE FILE_NAME ● USE pragma works like REQUIRE: – It looks for FILE_NAME in @INC – It includes the required source it into the main program ● Main difference: – FILE_NAME is included before anything else is done in the programwww.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. DO FILE_NAME ● DO pragma looks at @INC and, if it cannot find the file, does nothing ● If it is found, the code from FILE_NAME is inserted into the program that executes DO as a block of code ● Lexical variables in FILE_NAME will not be seen from in the program that executes DOwww.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. MyMathPL Application source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. MyMathPL File Structure /MyMathPL Constants.pm /Add AddTwo.pm AddThree.pm AddFour.pm /Mult MultTwo.pm MultFour.pm /Subt SubtTwo.pm SubtThree.pm SubtFour.pmwww.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. Constants.PM package MyMathPL::Constants; use strict; use warnings; sub app_name { return MyMathPL; } sub app_version { return 1.0; } sub company_name { return XYZ, LLC; } ## remember this 1! 1; source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. AddTwo.PM package MyMathPL::Add::AddTwo; ## this is the root directory where MyMathPL folder is placed. Set it to where the ## the MyMathPL folder is. use lib ""; use strict; use warnings; use MyMathPL::Constants; sub add2 { return $_[0] + $_[1]; } sub info { print This is module AddTwo of , MyMathPL::Constants::app_name(), application , version , MyMathPL::Constants::app_version(), "n"; } 1; source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. SubtTwo.PM package MyMathPL::Subt::SubtTwo; ## this is the root directory where MyMathPL folder is placed. Set it to where the ## the MyMathPL folder is. use lib ""; use strict; use warnings; use MyMathPL::Constants; sub subt2 { return $_[0] - $_[1]; } sub info { print This is module SubtTwo of , MyMathPL::Constants::app_name(), application , version , MyMathPL::Constants::app_version(), "n"; } 1; source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. MultTwo.PM package MyMathPL::Mult::MultTwo; ## this is the root directory where MyMathPL folder is placed. Set it to where the ## the MyMathPL folder is. use lib ""; use strict; use warnings; use MyMathPL::Constants; sub mult2 { return $_[0] * $_[1]; } sub info { print This is module MultTwo of , MyMathPL::Constants::app_name(), application , version , MyMathPL::Constants::app_version(), "n"; } 1; source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  32. 32. Testing MyMathPL use lib "D:/Dropbox/teaching/PythonPerl/s13/lectures/18/"; use warnings; use strict; use MyMathPL::Add::AddTwo; use MyMathPL::Add::AddThree; use MyMathPL::Add::AddFour; ############# Testing MyMathPL::Add::AddTwo ###################### MyMathPL::Add::AddTwo::info(); my $add2_n01 = MyMathPL::Add::AddTwo::add2(10, 12); my $add2_n02 = MyMathPL::Add::AddTwo::add2(10.5, 12.5); print $add2_n01, "n"; print $add2_n02, "n"; print "n"; ## The other modules can be tested similarlywww.youtube.com/vkedco www.vkedco.blogspot.com
  33. 33. Exporter Packageswww.youtube.com/vkedco www.vkedco.blogspot.com
  34. 34. Exporting Symbols ● If you call any functions (e.g., add2 or subt4) without the fully qualified name (e.g., MyMathPL::Add::AddTwo::add2(1, 2)), you will see something like: Undefined subroutine &main::add2 called at line X ● If you do not want to qualify the subroutine names, you need to export them from appropriate packages ● We can modify the Perl modules to export these symbols (in the source code the exporter modules end with Ex.pm)www.youtube.com/vkedco www.vkedco.blogspot.com
  35. 35. package MyMathPL::Add::AddTwoEx; AddTwoEx.PM ## this is the root directory where MyMathPL folder is placed. use lib ""; use strict; use warnings; use MyMathPL::Constants; ## This is the exporting stuff use Exporter; use base Exporter; our @EXPORT = qw(add2); ## export add2 symbol so that is is seen in other modules sub add2 { return $_[0] + $_[1]; } sub info { print This is module AddTwoEx of , MyMathPL::Constants::app_name(), application , version , MyMathPL::Constants::app_version(), "n"; } 1; source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  36. 36. Testing MyMathPL with Exporter Modules use lib ""; ## Set this to the folder where MyMathPL folder is located. use warnings; use strict; use MyMathPL::Add::AddTwoEx; use MyMathPL::Add::AddThreeEx; use MyMathPL::Add::AddFourEx; ############# Testing MyMathPL::Add::AddTwoEx ###################### MyMathPL::Add::AddTwoEx::info(); ## info() is not exported so it must be fully qualified my $add2_n01 = add2(10, 12); my $add2_n02 = add2(10.5, 12.5); print $add2_n01, "n"; print $add2_n02, "n"; print "n"; source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
  37. 37. Reading & References ● http://docs.python.org/py3k/tutorial/modules.html ● www.cpan.org ● http://perldoc.perl.org/functions/package.htmlwww.youtube.com/vkedco

×