Perl module

A Perl module is a separate component of a computer program, written in Perl.

A module has its source code in a module file ending pm, in turn, includes a package, so a separate namespace (which is not mandatory, however ). ; the structure of the package name (and thus the namespace name) The file system reflects (so that the net :: FTP module in the file Net / FTP.pm located ). In addition, a Perl module to be used with namespace as a class, if object-oriented programming is applied ( in Perl namespaces are equivalent to classes, but must not be used as such).

A collection of modules with associated documentation, build scripts, etc. form a distribution. The Perl community maintains a large collection of packages etc. on CPAN.

Perl as a programming language allows multiple programming styles. One can find both a module that procedural ( " conventional " similar to C) is written ( for example, Test -Simple ), as well as modules with object-oriented interfaces (eg XML :: Parser), and both the one and the other can fulfill their tasks efficiently, since it inter alia arrives on the context: In conjunction with XML an object-oriented module interface is much more useful than, for example, in a mathematical function library. Modules can be used as a mixin methods or a pragma (eg, strict), which then has a direct effect on the program. In addition, modules can be used to change the syntax of the language itself. Modules usually only have an effect on the visibility region in which they are loaded ( usually a file ).

Perl modules, it is common to have embedded documentation as Plain Old Documentation. POD is flexible enough to write articles, websites or even books (eg Programming Perl ), as opposed to javadoc, which specializes in the documentation of classes. Normally, the documentation of the structure of a manpage follow.

The code on this page is based on Perl 5.10.0 and should therefore also with Perl > 5.10.0 can be performed. For example, with Perl 5.6 can not run the particular namespace examples. It is ( exceptions are marked ) the use of the pragma strict assumed, which has cleaner code execution.

Examples

A " Hello World " function can be realized in different ways with modules, it is not mandatory, the function in a module (in Perl rather namespace; package) to pack (as opposed to Java, but rather a function defined everywhere and are used to put ( in modules also about integration with use) Nevertheless, it is common for reasons of clarity, the elements of a Perl module in a namespace (package ) a HelloWorld function would look like this. .:

Module1 package; sub hello () {print "Hello, World \ n"; } procedural approach

In hello.pm the hello () function is defined and implemented, which is executed in the main script main.pl.

Main.pl:

#! / usr / bin / perl -w use strict; # Pragma strict is used, so that a particularly stringent test is performed use world :: hello; # Hello.pm world / hello.pm, the base - directories, so the directory where # world / could lie in a path variable called @ INC are given, which also contains the current directory #   # either hello :: hello (); # or hello- > hello ( " world "); hello.pm:

Package hello;   # Lines with an "= " featuring integrated PO documentation. # A POD section ends with "= cut" and can be positioned anywhere.   = head1 NAME Hello: World - THE Hello World implementation.   = head1 SUMMARY   Use Hello :: World; hello (); hello ( " world ");   = head2 Functions   The following functions are available:   = head3 hello ()   hello () hello ($ etw_anderes )   = head1 Author   J. Random Hacker   = cut   sub hello ( $ ) {      my $ etw_anderes = shift @ _;      defined $ etw_anderes? print "Hello $ etw_anderes \ n": print "Hello world \ n"; }   return 1; # Each Perl module must return a true value to the compiler, otherwise there is a Error Object-oriented approach

Object-oriented solutions in all languages ​​require more code and have a lower efficiency in execution. During development, the object-orientation, however, is often more efficient than a procedural solution. In Perl, object orientation is realized by references ( scalars with a memory address ) to code and a hash.

Main.pl:

#! / usr / bin / perl -w use strict; Use Hello :: World; # Module is in Hello / World.pm   my $ hello = new Hello :: World; # Create object   $ hello- > gib_begr_aus; # Print "Hello world"   $ hello = $ hello- > aendere_ding ( " galaxy "); # To welcoming object in " Galaxy" change back and save the modified object   my $ LIMIT = $ hello- > begr_in_string; Save # welcome ...   print $ LIMIT; # And output World.pm:

Package Hello :: World; # In the object-oriented approach, the namespace must be defined, as in Perl, each class has a namespace   use strict; use warnings;   # # Possible # POD content #   sub new ($ $) # prototype of the constructor {      my $ class = shift @ _;      if ( exists $ _) # If not otherwise specified, welcome "world"      {           my $ rem = $ _;      else }      {           my $ thing = "world";      }      my $ self = { rem => $ thing }; # Hash reference with data generated ...      bless ( $ self, $ class ); # ... With the class name " blessed" ...      return $ self; # ... And returned }   sub aendere_ding ($ $) Change # The thing that is welcomed {      my $ self = shift @ _; Accept # object      my $ rem = shift @ _;      $ self -> { thing } = $ thing; # Manipulate data ...      return $ self; # ... Return modified object }   sub gib_begr_aus ( $) # Print the greeting generated on STDOUT {          my $ self = shift @ _; # Object is assumed again ...          print (" Hello $ self -> { rem } \ n " ); # ... And output. }   sub begr_in_string ( $) # The welcome return generated as a string ( eg, use of print () {          my $ self = shift @ _;          return " Hello $ self -> { rem } \ n "; } Namespaces and Scopes

A Perl program at runtime, unless otherwise declared, the namespace main - a function func1 () can be called both as func1 () as well as main :: func1 (); the value of $ var can be found both over $ var as well as $ main :: var. It can at any time be declared and used new namespaces. Variables that have been declared with my not have a namespace belonging, and exist globally, but only within a file. Variables that are declared with our have a namespace belonging and exist entirely global, as well as via file boundaries, although it is contributing in a different file than the one with the declaration (ie the the including file) with scope resolution operator :: and the namespace must be called. In one file a variable from our foreign namespace must only be fully qualified to be called ( with :: and namespace ) if the calling namespace already exists a our or my variable of the same name.

Package namensraum1; my $ var = "foo"; # With my declared variables are not in any namespace.                   # A Call by $ namensraum1 :: var has no success, anywhere with $ var, however. our $ var2 = "bar"; # Our declared a bound to the namespace variable (our is only necessary with strict)   $ namensraum2 :: var3 = 42; # Namespace does not exist yet, but this created equal. (only if strict is not used ) our $ namensraum2 :: var4 = 21; # With and without strict: error my $ namensraum2 :: var5 = 23:11; # Error because my is not namespace- bound   sub namensraum2 :: hello_w () {print ("Hello World \ n " ); } # If strict both with and without see also

  • Java Archive
  • Object-Oriented Programming
  • Perl
642068
de