Thanks for helping me! this was exactly the solution I was looking for.
Hi, is the any way to catch an error while initializing pm like "use SOME::PM" or call "use ..." only when corresponding library is installed?
I am trying to implement support of GZIP to the script. The idea is to switch compression/decompression ON or OFF with the flag. The usual way will be to initialize corresponding library in the beginning and just use it when needed:
#!/usr/bin/perl use IO::Compress::Gzip qw(gzip $GzipError); use Getopt::Long; #usage: $0 --gzip my $useGZ; my $options = &Getopt::Long::GetOptions( 'gzip|z' => \$useGZ ); my $OUT_FHs; my $out_file = "test.txt"; if($useGZ) { $OUT_FHs = new IO::Compress::Gzip ("$out_file.gz") or die $GzipError; } else { open $OUT_FHs, '>', $out_file or die "Can't open $out_file for writin +g; $!\n"; } print $OUT_FHs "printing to test file\n"; close($OUT_FHs); exit;
The problem came from usual site - on my test system everything works, but on the user's system it doesn't and exits with an error:
Can't locate IO/Compress/Gzip.pm in @INC (@INC contains: /usr/local/li +b64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/sh +are/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .)
Obviously, the module is not installed or not in PATH on the user's system. The Perl doesn't allow to go further with the execution of the script even if I am not using the compression/decompression functionality.
Unfortunately the user can't change or install anything on the system so I need to find a way around to enable GZIP support when needed but to ignore error message from module initialization if it's not installed on the system.
In reply to error handling by pm initialization by homeveg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |