0
if (a == b)
{
    eval "require IO::Compress::Gzip qw(gzip)";
}

This is what I did to include gzip based on a condition, but at run time it gives error as below

Can't locate object method "gzip" via package "IO::Handle" (perhaps you forgot to load "IO::Handle"?)

Any help please? thanks.

1 Answer 1

3

To conditionally include a module, use the if pragma

use if ($x == $y), 'IO::Compress::Gzip' => qw(gzip);

Just note that the variables in the CONDITION will have to be package level and initialized in a BEGIN block.

Alternatively, you can use the following:

if ($x == $y) {
    require IO::Compress::Gzip;
    IO::Compress::Gzip->import('gzip');
}
Sign up to request clarification or add additional context in comments.

6 Comments

Miller, I used your second solution, got the same error, let me try "use". I am using perl, v5.6.1 Thanks.
Does doing a basic use statement give that error? use IO::Compress::Gzip qw(gzip); I suspect that there is something else going on with your environment.
yes, use IO::Compress::Gzip qw(gzip $GzipError); works, but not your alternative solution, may be Perl version 5.6.1 don't have that support?
Perl 5.6.1 is more than 13 years old. But I don't think that's related to your error.
Perl 5.6.1 isn't even the latest Perl 5.6.x release!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.