Tutorial Addendum on Perl - Allotment A - perlmod - Perl Modules

 31 December 18:00   

    



    



    

The calling program, IncRequireTest.pl,

    

 

    

#- IncRequireTest.pl

    

#- Absorb (c) 1995 by Dr. Yang

    

#

    

print("
Testing require()...
");

    

require("MyRequireLib.inc");

    

require("MyRequireLib.inc");

    

print("Author = $author
");

    

&myRequireSub();

    

print("
Testing do()...
");

    

do("MyDoLib.inc");

    

do("MyDoLib.inc");

    

print("Author = $author
");

    

&myDoSub();

    

exit;

    



    



    

The achievement matches my expectation. The additional require() alarm didn t do any execution.

    

 

    

Testing require()...

    

Printing from MyRequireLib.inc...

    

Author = Dr. Yang

    

Printing from myRequireSub()...

    

Testing do()...

    

Printing from MyDoLib.inc...

    

Printing from MyDoLib.inc...

    

Author = Yang

    

Printing from myDoSub()...

    



    



    



    

Using "package" to Make New Name Spaces

    



    

Name Space: A accumulation assemblage acclimated to abstract identifiers from altered parts

    

of the antecedent code. Name amplitude is aswell alleged package. There are several basic

    

rules about Name spaces:

    



        

  • There is a absence Name amplitude alleged "main". So after any appropriate coding,

        

    every band of the antecedent cipher is in the "main" Name space.

        



  •     

  • You can use "package Name _space;" to avenue the accepted Name space, and

        

    to access into the defined Name space. The accepted Name amplitude ends at the

        

    end of the accepted cipher block, or end of the accepted file.

        



  •     

  • Identifiers from an additional Name amplitude can be accessed by prefixing the identifier

        

    with the Name amplitude Name followed by a bifold colon, ::.

        



  •     

  • The appropriate attribute __PACKAGE__ contains the Name of the accepted Name space.

        



  •     



    



    

To verify those rules, I wrote the afterward program, PackageTest.pl,

    

 

    

#- NameSpaceTest.pl

    

#- Absorb (c) 1995 by Dr. Yang

    

#

    

$j = "Java"; # starting with main

    

$f = "Fortran";

    

$p = "Perl";

    

$c = "C++";

    

@list = ($j, $f, $p, $c);

    

print("
In ",__PACKAGE__,"...
");

    

print("list = ",join( , ,@list),"
");

    

amalgamation Calendar; # entering Calendar

    

$j = "January";

    

$f = "February";

    

$m = "March";

    

$a = "April";

    

@list = ($j, $f, $m, $a);

    

print("
In ",__PACKAGE__,"...
");

    

print("list = ",join( , ,@list),"
");

    

{

    

amalgamation Fruit; # entering Fruit

    

$p = "peach";

    

$o = "orange";

    

$m = "mango";

    

$a = "apple";

    

@list = ($p, $o, $m, $a);

    

print("
In ",__PACKAGE__,"...
");

    

print("list = ",join( , ,@list),"
");

    

} # catastrophe Fruit

    

# aback to Calendar

    

print("
Back in ",__PACKAGE__,"...
");

    

print("list = ",join( , ,@list),"
");

    

amalgamation Fruit; # entering Fruit

    

print("
Back in ",__PACKAGE__,"...
");

    

print("main::p = ",$main::p,"
");

    

print("Fruit::p = ",$Fruit::p,"
");

    

print("list = ",join( , ,@list),"
");

    

print("main::list = ",join( , ,@main::list),"
");

    

print("Calendar::list = ",join( , ,@Calendar::list),"
");

    

exit;

    



    



    

The achievement matches my expectation:

    

 

    

In main...

    

list = Java, Fortran, Perl, C++

    

In Calendar...

    

list = January, February, March, April

    

In Fruit...

    

list = peach, orange, mango, apple

    

Back in Calendar...

    

list = January, February, March, April

    

Back in Fruit...

    

main::p = Perl

    

Fruit::p = peach

    

list = peach, orange, mango, apple

    

main::list = Java, Fortran, Perl, C++

    

Calendar::list = January, February, March, April

    



    



    

 


Tags: space, author, package, entering, orange, february, january, fruit, printing, print

 print, package, space, fruit, @list, calendar, current, january, february, march, author, mydolib, mango, peach, orange, entering, fortran, printing, myrequirelib, , name space, list join, join @list, print list, calendar list, mydolib inc, list january, january february, february march, in package, exit the, printing from, current name, print in, myrequirelib inc, print list join, list join @list, list january february, calendar list january, print in package, current name space, january february march, java fortran perl, peach orange mango, list java fortran, package fruit entering, printing from mydolib, author author &, inc print author, perlmod perl modules, print author author , fruit entering fruit,

Share Tutorial Addendum on Perl - Allotment A - perlmod - Perl Modules:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  perl