·  PERL - Syntax - Other – Packages


- What is package:

  - In Perl, package is part of code between keyword 'package' and the end of file or next keyword 'package'.

  - This means that single file can contain one or more packages.

  - Package name is string which follows keyword 'package'.

  - Name of the file which contains package(s) can be choosen arbitrarily, independently from name(s) of package(s).

  - If no package is defined at the begining of the file, perl automaticly inserts statement 'package main'.

  - If you have used 'package ivor;' in multiple places in a single file or even in multiple .pl files, perl will see all that code

    as belonging to single 'package ivor'.


- Why were packages invented:

  - Packages are invented so that you can use the same variable and function names in different chunks of code.

  - Full name of such variable or function is then automaticly defined by placing package name at the begining.

  - So if you have variable ivor inside package myPackage, then full variable name would be 'myPackage::ivor'.

  - Now if some perl code can see packages myPackage and hisPackage, and they both have variable ivor defined in them,

    then ivor variable from package myPackage is referenced by using 'myPackage::ivor'.


- Keyword 'use':

  - If you want to reference more then one variable or function defined in package myPackage, you can use statement 'use

    myPackage;' to tell perl to automaticly put myPackage:: at the begining of all the names that follow.

  - You can also use multiple statements like  'use myPackage;', 'use hisPackage;' one after another.

  - Then when you write some variable or function name, perl will try to find it in packages myPackage and hisPackage.

  - If that name exists only in package myPackage, it will insert myPackage:: at the begining of the name.

  - If that name exists in both packages, it will report error because it wouldn't know which variable are you refering to.

  - If that name doesn't exist in any of those two packages, but existst in current package, it will insert the name of the

    current package at the begining of the variable or function name.

  - If that name doesn't exist in any of those two packages, and it also doesn't exist in current package, but exists in some

    other package that perl can see, error will be reported because for such package we didn't use keyword 'use' and perl

    wants us to define such names in full by placing package name at the begining.


- @INC:

  - When you execute perl script, perl sees everything that you have written as packages.

  - Perl collects all packages that it can find in .pl files in directories defind by @INC but not in their's subdirectories.

  - You can add new directory in @INC using: push(@INC,'H:/Installed/Programming/Workspace/TS');

  - To tell perl to look in current directory use: push(@INC,'.')  (What is current directory);

  - To see in which directories perl looks for packages use:   print "@INC";

  - If you want to use package ivor which is defined in 'C:\Temp\SubDir\SomeFile.pl' and @INC contains 'C:\Temp', then

    you should use: use SubDir::ivor, instead only use ivor, so that perl can find the file with the package inside it.

    (What is SubDir is subdirectory of multiple directories in @INC).