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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.