NAME
File::Package - test load a pm and import symbols without eval and $@ misbehavoirs
SYNOPSIS
##########
# Subroutine interface
#
use File::Package qw(is_package_loaded load_package);
$yes = is_package_loaded($package, $program_module);
$error = load_package($program_module);
$error = load_package($program_module, @import);
$error = load_package($program_module, [@package_list]);
$error = load_package($program_module, @import, [@package_list]);
##########
# Class Interface
#
use File::Package;
$yes = is_package_loaded($package, $program_module);
$error = File::Package->load_package($program_module);
$error = File::Package->load_package($program_module, @import);
$error = File::Package->load_package($program_module, [@package_list]);
$error = File::Package->load_package($program_module, @import, [@package_list]);
DESCRIPTION
In a perfect Perl, everything would behave exactly the same running under "eval". Many times the reason to use an "eval" is the anticipation that the expression may die. When that happens, a perfect Perl would have deposited all the output fromm the "warn" and "die" in "$@". Maybe you have a perfect Perl. However, it is shocking that there are some Perls on some platforms out in the wild that are mutants and are not perfect.
A "require" under eval works just fine just to see if a program will load or not. If working locally, you can simply devise a quick debug setup and track down the problem. However, when running tests remotely, on different remote platforms, running continuously unattended where uptime is important, or any number of situations it is very helpful to have meaningful error messages when a problem arise.
Thus, the reason to run under "eval" is not only to avoid the "die" but also to pick up the error message returned by "eval" in "$@". In certain situations it is extremely critical to obtain reliable error messages when a failure occurs.
Well, a "eval "require $program_module"" failure returns a reasonble looking "$@" except for one small thing. Not all the warnings make it to "$@" at least on one Perl, probably more. And there can be quite a few warnings when loading a broken program module. It would be nice if everyone could update to a Perl where the "eval" deposits all the warnings in "$@". But as the acient proverb says, "If wishes were horses, beggers would ride.".
One workaround is to catch the warnings with "$SIG{__WARN__}" when running the "require" under a "eval". This collects all the warnings which is good. Now when a load fails, the program does not die, it gracefully collects all the warnings and logs them or ships back.
Now try the "import" under "eval" and pick up the error messages. The "import" and "eval" is big time "failure to communicate" aka the movie "Cool Hand Luke". The "import" uses the caller stack to determine where to stuff the symbols and there is a lot of "Carp" "croak" gyrations such as making "import" look like "use", trapping "warnings" and "dies". The "eval" takes off on its own caller stack which to quote President Bush: "is not helpful".
The "import" uses the "croak" instead of "die" directly or else any efforts to get meaningfull error messages would be dead on arrival. Perl is designed so that it is nearly impossible to avoid a die unless running under a "eval". A workaround is hooking in a "croak" that does not die and collecting the error messages.
Subroutines
is_package_loaded
$package = is_package_loaded($program_module, $package)
The "is_package_loaded" subroutine determines if the "$package" is present and the "$progarm_module" loaded. If "$package" is absent, 0 or '', "$package" is set to the "program_module".
load_package
$error = load_package($program_module, @import, [@package_list]);
The "load_package" subroutine attempts to capture any load problems by loading the package with a "require " under an eval and capturing all the "warn" and $@ messages.
If the "$program_module" load is successful, the checks that the packages in the @package list are present. If @package list is absent, the "$program_module" uses the "program_module" name as a list of one package. Although a program module and package have the same name syntax, they are entirely different. A program module is a file. A package is a hash of symbols, a symbol table. The Perl convention is that the names for each are the same which enhances the appearance that they are the same when in fact they are different. Thus, a program module may have a single package with a different name or many different packages.
Finally the "$program_module" subroutine will import the symbols in the "@import" list. If "@import" is absent "$program_module" subroutine does not import any symbols; if "@import" is '', all symbols are imported. A "@import" of 0 usually results in an "$error".
The "$program_module" traps all load errors and all import "Carp::Crock" errors and returns them in the "$error" string.
One very useful application of the "load_package" subroutine is in test scripts. If a package does load, it is very helpful that the program does not die and reports the reason the package did not load. This information is readily available when loaded at a local site. However, it the load occurs at a remote site and the load crashes Perl, the remote tester usually will not have this information readily available.
Other applications include using backup alternative software if a package does not load. For example if the package 'Compress::Zlib' did not load, an attempt may be made to use the gzip system command.
BUGS
The "load_package" cannot load program modules whose name contain the '-' characters. The 'eval' function used to trap the die errors believes it means subtraction.
REQUIREMENTS
Coming.
DEMONSTRATION
#########
# perl Package.d
###
~~~~~~ Demonstration overview ~~~~~
Perl code begins with the prompt
=>
The selected results from executing the Perl Code follow on the next lines. For example,
=> 2 + 2
4
~~~~~~ The demonstration follows ~~~~~
=> use File::Package;
=> my $uut = 'File::Package';
=> ##################
=> # Good Load
=> #
=> ###
=> my $error = $uut->load_package( 'File::Basename' )
''
=> $error = $uut->load_package( 'File::BadLoad' )
'Cannot load File::BadLoad
syntax error at E:/User/SoftwareDiamonds/installation/t/File/File/BadLoad.pm line 13, near "$FILE "
Global symbol "$FILE" requires explicit package name at E:/User/SoftwareDiamonds/installation/t/File/File/BadLoad.pm line 13.
Compilation failed in require at (eval 12) line 1.
Scalar found where operator expected at E:/User/SoftwareDiamonds/installation/t/File/File/BadLoad.pm line 13, near "$FILE"
(Missing semicolon on previous line?)
'
=> $uut->load_package( 'File::BadPackage' )
'# File::BadPackage file but package(s) File::BadPackage absent.
'
=> $uut->load_package( 'File::Multi' )
'# File::Multi file but package(s) File::Multi absent.
'
=> $error = $uut->load_package( 'File::Hyphen-Test' )
'Cannot load File::Hyphen-Test
syntax error at (eval 15) line 1, near "require File::Hyphen-"
Warning: Use of "require" without parens is ambiguous at (eval 15) line 1.
'
=> ##################
=> # No &File::Find::find import baseline
=> #
=> ###
=> !defined($main::{'find'})
'1'
=> ##################
=> # Load File::Find, Import &File::Find::find
=> #
=> ###
=> $error = $uut->load_package( 'File::Find', 'find', ['File::Find'] )
''
=> ##################
=> # &File::Find::find imported
=> #
=> ###
=> defined($main::{'find'})
'1'
=> ##################
=> # &File::Find::finddepth not imported
=> #
=> ###
=> !defined($main::{'finddepth'})
'1'
=> ##################
=> # Import error
=> #
=> ###
=> $uut->load_package( 'File::Find', 'Jolly_Green_Giant')
'"Jolly_Green_Giant" is not exported by the File::Find module
Can't continue after import errors at D:/Perl/lib/Exporter/Heavy.pm line 127
Exporter::heavy_export('File::Find', 'main', 'Jolly_Green_Giant') called at D:/Perl/lib/Exporter.pm line 45
Exporter::import('File::Find', 'Jolly_Green_Giant') called at (eval 9) line 81
File::Package::load_package('File::Package', 'File::Find', 'Jolly_Green_Giant') called at E:\User\SoftwareDiamonds\installation\t\File\Package.d line 195
'
=> ##################
=> # &File::Find::finddepth still no imported
=> #
=> ###
=> !defined($main::{'finddepth'})
'1'
=> ##################
=> # Import all File::Find functions
=> #
=> ###
=> $error = $uut->load_package( 'File::Find', '')
''
=> ##################
=> # &File::Find::finddepth imported
=> #
=> ###
=> defined($main::{'finddepth'})
'1'
QUALITY ASSURANCE
Running the test script "package.t" verifies the requirements for this module.
The <tmake.pl> cover script for Test::STDmaker automatically generated the "package.t" test script, "package.d" demo script, and "t::File::Package" STD program module POD, from the "t::File::Package" program module contents. The "t::File::Package" program module is in the distribution file File-Package-$VERSION.tar.gz.
NOTES
AUTHOR
The holder of the copyright and maintainer is
<support@SoftwareDiamonds.com>
COPYRIGHT NOTICE
Copyrighted (c) 2002 Software Diamonds
All Rights Reserved
BINDING REQUIREMENTS NOTICE
Binding requirements are indexed with the pharse 'shall[dd]' where dd is an unique number for each header section. This conforms to standard federal government practices, 490A (the 3.2.3.6 entry in the STD490A manpage). In accordance with the License, Software Diamonds is not liable for any requirement, binding or otherwise.
LICENSE
Software Diamonds permits the redistribution and use in source and binary forms, with or without modification, provided that the following conditions are met:
1 Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2 Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
SOFTWARE DIAMONDS, http::www.softwarediamonds.com, PROVIDES THIS SOFTWARE 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWARE DIAMONDS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING USE OF THIS SOFTWARE, EVEN IF ADVISED OF NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE POSSIBILITY OF SUCH DAMAGE.
SEE ALSO
Docs::Site_SVD::File_Package
Test::STDmaker
Title Page
Software Version Description
for
File::Package - test load a pm and import symbs without eval and $@ misbehavoirs
Revision: E
Version: 0.06
Date: 2004/04/26
Prepared for: General Public
Prepared by: SoftwareDiamonds.com E<lt>support@SoftwareDiamonds.comE<gt>
Copyright: copyright © 2003 Software Diamonds
Classification: NONE
1.0 SCOPE
This paragraph identifies and provides an overview of the released files.
1.1 Identification
This release, identified in 3.2, is a collection of Perl modules that extend the capabilities of the Perl language.
1.2 System overview
One very useful test of the installation of a package is whether or not the package loaded. If it did not load, the reason it did not load is helpful diagnostics and may be used to programically (automatically) take corrective action.
The load_package method attempts to capture any load problems by loading the package with a "require " under an eval and capturing all the "warn" and $@ messages. The error messages are returned with a warn instead of die so that the using program may take the appropriate action such as reporting the errors back to the author when used in test software or perhaps falling back on a system 'gzip' command when the 'Compress::Zlib' module fails to load.
1.3 Document overview.
This document releases File::Package version 0.06 providing a description of the inventory, installation instructions and other information necessary to utilize and track this release.
3.0 VERSION DESCRIPTION
All file specifications in this SVD use the Unix operating system file specification.
3.1 Inventory of materials released.
This document releases the file
File-Package-0.06.tar.gz
found at the following repository(s):
http://www.softwarediamonds/packages/
http://www.perl.com/CPAN/authors/id/S/SO/SOFTDIA/
Restrictions regarding duplication and license provisions are as
Copyright.
copyright © 2003 Software Diamonds
Copyright holder contact.
603 882-0846 E<lt>support@SoftwareDiamonds.comE<gt>
License.
Software Diamonds permits the redistribution and use in source and
binary forms, with or without modification, provided that the
following conditions are met:
1 Redistributions of source code, modified or unmodified must
retain the above copyright notice, this list of conditions and
the following disclaimer.
2 Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
SOFTWARE DIAMONDS, http://www.SoftwareDiamonds.com, PROVIDES THIS
SOFTWARE 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
SOFTWARE DIAMONDS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING USE OF THIS SOFTWARE, EVEN IF ADVISED OF
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE POSSIBILITY
OF SUCH DAMAGE.
3.2 Inventory of software contents
The content of the released, compressed, archieve file, consists of the following files:
file version date comment
------------------------------------------------------------ ------- ---------- ------------------------
lib/Docs/Site_SVD/File_Package.pm 0.06 2004/04/26 revised 0.05
MANIFEST 0.06 2004/04/26 generated, replaces 0.05
Makefile.PL 0.06 2004/04/26 generated, replaces 0.05
README 0.06 2004/04/26 generated, replaces 0.05
lib/File/Package.pm 1.16 2004/04/26 revised 1.15
t/File/Package.d 0.03 2004/04/26 revised 0.02
t/File/Package.pm 0.01 2004/04/10 unchanged
t/File/Package.t 0.12 2004/04/26 revised 0.11
t/File/File/BadLoad.pm 0.01 2004/04/10 unchanged
t/File/File/BadPackage.pm 0.01 2004/04/10 unchanged
t/File/File/Hyphen-Test.pm 1.15 2004/04/10 unchanged
t/File/File/Multi.pm 1.15 2004/04/10 unchanged
t/File/Test/Tech.pm 1.21 2004/04/26 revised 1.17
t/File/Data/Secs2.pm 1.18 2004/04/26 revised 1.15
t/File/Data/SecsPack.pm 0.03 2004/04/26 new
t/File/Data/Startup.pm 0.03 2004/04/26 new
3.3 Changes
The file names from 0.05 were changed as follows:
return if $file =~ s=lib/File/FileUtil.pm=lib/File/Package.pm=;
return if $file =~ s=t/File/FileUtil/FileUtil.t=t/File/package.t=;
Changes to past revisions are as follows:
Test-TestUtil-0.01
Originated
Test-TestUtil-0.02
Correct failure from Josts Smokehouse"
<Jost.Krieger+smokeback@ruhr-uni-bochum.de> test run
t/Test/TestUtil/TestUtil....Bareword "fspec_dirs" not allowed while
"strict subs" in use at
/net/sunu991/disc1/.cpanplus/5.8.0/build/Test-TestUtil-0.01/blib/lib/Test/TestUtil.pm line 56.
Changed line 56 from
my @dirs = (fspec_dirs) ? $from_package->splitdir( $fspec_dirs ) : ();
to
my @dirs = ($fspec_dirs) ? $from_package->splitdir( $fspec_dirs ) : ();
This error is troublesome since the test passed on my system using
Active Perl under Microsoft NT. It should never have passed. This
error is in a core method, fspec2fspec, that changes file
specifications from one operating system to another operating
system. This method has been in service unchanged for some time.
Test-TestUtil-0.03
Correct failure from Josts Smokehouse"
<Jost.Krieger+smokeback@ruhr-uni-bochum.de> test run
PERL_DL_NONLAZY=1 /usr/local/perl/bin/perl "-MExtUtils::Command::MM"
"-e" "test_harness(0, 'blib/lib', 'blib/arch')"
t/Test/TestUtil/TestUtil.t t/Test/TestUtil/TestUtil....# Test 18
got: '$VAR1 = ''; ' (t/Test/TestUtil/TestUtil.t at line 540 fail
#17) # Expected: '$VAR1 = '\\=head1 Title Page
The pm2datah method is not returning any data for Test 18. This
will also cause the test of pm2data, test 19 to fail. The
pm2datah is searching for the string "\n__DATA__\n".
The "\n" character on Perl is a logical end of line character
sequence. The "\n" end of line is different on Mr. Smokehouse's Unix
operating system than on my Windows NT operating system. The test
file was created under MSWin32 and uses a MSWin32 "\n". Under UNIX,
pm2datah method will look for the Unix "\n" and there will not be
any.
Changed "\n__DATA__\n" to /[\012\015]__DATA__/.
During the clean-up for CPAN, broke the format_hash_table method
for tables in hash of hash format. Fixed the break, added test 29 to
the t/Test/TestUtil/TestUtil.t test script for this feature, and
added a discusssion of this feature in POD discription for
format_hash_table
Test-TestUtil-0.04
item our old friend visits again - DOS and UNIX text file
incompatibility
This impacts other modules. We have to examine all modules for this
portability defect and correct any found defects.
Correct failure from Josts Smokehouse"
<Jost.Krieger+smokeback@ruhr-uni-bochum.de> and Kingpin
<mthurn@carbon> test runs.
On Mr. Smokehouse's run email the got: VAR1 clearly showed extra
white space line that is not present in the expected: VAR1. In Mr.
Kingpin's run the got: VAR1 and expected: VAR1 look visually the
same. However, the Unix found a difference(s) and failed the test.
For Mr. Smokehouse's run:
PERL_DL_NONLAZY=1 /usr/local/bin/perl "-MExtUtils::Command::MM" "-e"
"test_harness(0, 'blib/lib', 'blib/arch')"
t/Test/TestUtil/TestUtil.t t/Test/TestUtil/TestUtil....NOK 18# Test
18 got: '$VAR1 = '\\=head1 Title Page
Software Version Description
for
File::Package - test load a pm and import symbs without eval and $@ misbehavoirs
Revision: E
[snip]
(t/Test/TestUtil/TestUtil.t at line 565 fail #17) # Expected: '$VAR1
= '\\=head1 Title Page
Software Version Description
for
File::Package - test load a pm and import symbs without eval and $@ misbehavoirs
What we have before, was a totally "failure to communicate." aka
Cool Hand Luke. VAR1 was empty. Now VAR1 has something. It is not
completely dead. One probable cause is the Unix operating system
must be producing two Unix \012 new lines for a Microsoft single
newline \015\012. Without being able to examine the test with a
debugger, the only way to verify this is to provide the fix and see
if the problem goes away when this great group of testers try for
the fourth time.
Revised fin method to take a handle, change pm2datah method
handle, $fh, to binary by adding a binmode $fh statement, and
pass the actual thru the fin method for test 18.
Use fin($fh) to read in the data for pm2data, test 19 Unit Under
Test (UUT), instead of using the raw file handle.
The fin method takes any \015\012 combination and changes it into
the logical Perl new line, "\n", for the current operating system.
File-FileUtil-0.01
Broke away all the file related methods from Test::TestUtil and created this module File::FileUtil so the module name is more descriptive of the methods within the module.
At 02:44 AM 6/14/2003 +0200, Max Maischein wrote: Perl, as Perl already does smart newline handling, (even though with the advent of 5.8 even Unix-people have to learn the word "binmode" now :-))
The only place where I see Perl does smart newline handling is the crlf IO displine introduce in Perl 5.6. The File::FileUtil has a use 5.001 so that 5.6 Perl built-ins cannot be used. Added comment to smart_nl that for users with 5.6 Perl that it may be better to use the built-in crlf IO discipline.
File-FileUtil-0.02
Added the method hex_dump.
File-FileUtil-0.03
test_lib2inc
Returns to parent directory of the first t directory going up
from the test script instead of the t directory.
findtroots
Added the function findtroots that returns the parent
directory of all the directories in @INC
File-Package-0.01
Removed the methods for loading a program module with the same name
from the "File::FileUtil" module to their own module "File::Package"
module. The module name is now much more descriptive of the routines
in the module.
File-Package-0.02
Replace the obsolete "File::FileUtil" with File::Packgage in the
test script "t\File\package.t".
File-Package-0.03
Added subroutine interfaces.
Added @import input to load_packages method
File-Package-0.04
Upgraded the 'tlib\Test\Tech' and changed the name of
'tlib\Data\strify' to 'tlib\Data\Secs2'. The new name is more
self-explanatory.
File-Package-0.05
The lastest build of Test::STDmaker expects the test library in the
same directory as the test script. Coordiated with the lastest
Test::STDmaker by moving the test library from tlib to t/File, the
same directory as the test script and deleting the test library
File::TestPath program module.
File-Package-0.06
Added "Carp::longmess", that dumps the call stack, to the
Carp::croak trap function.
File-Package-0.07
Expanded the description.
Under the Perl 5.6, Microsoft distribute, "Carp" program module,
"import" sends warings out using "&Carp::carp" function as well as
"Carp::croak" function. Adjust to also pick up these messages.
3.4 Adaptation data.
This installation requires that the installation site has the Perl programming language installed. There are no other additional requirements or tailoring needed of configurations files, adaptation data or other software needed for this installation particular to any installation site.
3.5 Related documents.
There are no related documents needed for the installation and test of this release.
3.6 Installation instructions.
Instructions for installation, installation tests and installation support are as follows:
Installation Instructions.
To installed the release file, use the CPAN module pr PPM module in
the Perl release or the INSTALL.PL script at the following web site:
http://packages.SoftwareDiamonds.com
Follow the instructions for the the chosen installation software.
If all else fails, the file may be manually installed. Enter one of
the following repositories in a web browser:
http://www.softwarediamonds/packages/
http://www.perl.com/CPAN/authors/id/S/SO/SOFTDIA/
Right click on 'File-Package-0.06.tar.gz' and download to a
temporary installation directory. Enter the following where $make is
'nmake' for microsoft windows; otherwise 'make'.
gunzip File-Package-0.06.tar.gz
tar -xf File-Package-0.06.tar
perl Makefile.PL
$make test
$make install
On Microsoft operating system, nmake, tar, and gunzip must be in the
exeuction path. If tar and gunzip are not install, download and
install unxutils from
http://packages.softwarediamonds.com
Prerequistes.
None.
Security, privacy, or safety precautions.
None.
Installation Tests.
Most Perl installation software will run the following test
script(s) as part of the installation:
t/File/Package.t
Installation support.
If there are installation problems or questions with the
installation contact
603 882-0846 E<lt>support@SoftwareDiamonds.comE<gt>
3.7 Possible problems and known errors
There is still much work needed to ensure the quality of this module as
4.0 NOTES
The following are useful acronyms:
.d extension for a Perl demo script file
.pm extension for a Perl Library Module
.t extension for a Perl test script file
POD Plain Old Documentation
2.0 SEE ALSO
File::Package
Docs::US_DOD::SVD