Click here to get back home

The 'if' module

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
The 'if' module Sisyphus 06-01-2006
|--> Re: The 'if' module Gunnar Hjalmars...06-01-2006
Posted by Sisyphus on June 1, 2006, 8:18 pm
Please log in for more thread options


D:\pscrpt\if>cat try.pl
use if $^O =~ /another/, MODULE => "Non::Existent";
print "OK\n";
D:\pscrpt\if>perl try.pl
Can't locate Non/Existent.pm in @INC (@INC contains: D:/perl58_M/5.8.8/lib
D:/perl58_M/site/5.8.8/lib D:/perl58_M/site/lib .) at
D:/perl58_M/5.8.8/lib/if.pm line 13.
BEGIN failed--compilation aborted at try.pl line 1.

Since $^O does not match "another", I expected there to be no attempt to
load the non-existent module (Non::Existent).

What's the correct usage of the 'if' module for this case where I want to
load Non::Existent iff $^O matches "another" ?

Cheers,
Rob



Posted by Gunnar Hjalmarsson on June 1, 2006, 9:30 pm
Please log in for more thread options


Sisyphus wrote:
> D:\pscrpt\if>cat try.pl
> use if $^O =~ /another/, MODULE => "Non::Existent";
> print "OK\n";
> D:\pscrpt\if>perl try.pl
> Can't locate Non/Existent.pm in @INC (@INC contains: D:/perl58_M/5.8.8/lib
> D:/perl58_M/site/5.8.8/lib D:/perl58_M/site/lib .) at
> D:/perl58_M/5.8.8/lib/if.pm line 13.
> BEGIN failed--compilation aborted at try.pl line 1.
>
> Since $^O does not match "another", I expected there to be no attempt to
> load the non-existent module (Non::Existent).
>
> What's the correct usage of the 'if' module for this case where I want to
> load Non::Existent iff $^O matches "another" ?

I have no idea.

Why not just do:

BEGIN {
if ($^O =~ /another/) {
require Non::Existent;
import Non::Existent;
}
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Paul Lalli on June 1, 2006, 9:55 pm
Please log in for more thread options


Sisyphus wrote:
> D:\pscrpt\if>cat try.pl
> use if $^O =~ /another/, MODULE => "Non::Existent";
> print "OK\n";

You've misread the documentation (which, admittedly, is sparse). You
are supposed to pass the module name as the second argument, and any
arguments you would normally pass to use or import() as the remaining
arguments. So this should be:

use if $^O =~ /another/, "Non::Existent";

> D:\pscrpt\if>perl try.pl
> Can't locate Non/Existent.pm in @INC (@INC contains: D:/perl58_M/5.8.8/lib
> D:/perl58_M/site/5.8.8/lib D:/perl58_M/site/lib .) at
> D:/perl58_M/5.8.8/lib/if.pm line 13.
> BEGIN failed--compilation aborted at try.pl line 1.
>
> Since $^O does not match "another", I expected there to be no attempt to
> load the non-existent module (Non::Existent).
>
> What's the correct usage of the 'if' module for this case where I want to
> load Non::Existent iff $^O matches "another" ?

Unfortunately, the above doesn't solve your problem. That's because
the pattern match is being evaluated in a list context, which means a
failure to match returns the empty list, not 0. So you actually only
passed one argument to if's subroutine. This causes the if module to
fail (albeit differently than your original attempt).

use if scalar($^O =~ /another/), "Non::Existent";

should do what you want.

Paul Lalli


Posted by Sisyphus on June 1, 2006, 10:39 pm
Please log in for more thread options



> Sisyphus wrote:
> > D:\pscrpt\if>cat try.pl
> > use if $^O =~ /another/, MODULE => "Non::Existent";
> > print "OK\n";
>
> You've misread the documentation (which, admittedly, is sparse). You
> are supposed to pass the module name as the second argument, and any
> arguments you would normally pass to use or import() as the remaining
> arguments. So this should be:
>
> use if $^O =~ /another/, "Non::Existent";

Yes, that was my first attempt but it produced the error:

Too few arguments to `use if' (some code returning an empty list in list
context?) at D:/perl58_M/5.8.8/lib/if.pm line 7.
BEGIN failed--compilation aborted at try.pl line 2.

>
> Unfortunately, the above doesn't solve your problem. That's because
> the pattern match is being evaluated in a list context, which means a
> failure to match returns the empty list, not 0. So you actually only
> passed one argument to if's subroutine. This causes the if module to
> fail (albeit differently than your original attempt).

Yep.

>
> use if scalar($^O =~ /another/), "Non::Existent";
>
> should do what you want.
>

It does indeed. Thanks Paul.

Cheers,
Rob



Posted by Paul Lalli on June 2, 2006, 7:52 am
Please log in for more thread options


Sisyphus wrote:
> > Sisyphus wrote:
> > > D:\pscrpt\if>cat try.pl
> > > use if $^O =~ /another/, MODULE => "Non::Existent";
> > > print "OK\n";
> >
> > You've misread the documentation (which, admittedly, is sparse). You
> > are supposed to pass the module name as the second argument, and any
> > arguments you would normally pass to use or import() as the remaining
> > arguments. So this should be:
> >
> > use if $^O =~ /another/, "Non::Existent";
>
> Yes, that was my first attempt but it produced the error:
>
> Too few arguments to `use if' (some code returning an empty list in list
> context?) at D:/perl58_M/5.8.8/lib/if.pm line 7.
> BEGIN failed--compilation aborted at try.pl line 2.

I do feel the need to point out that this error message told you
exactly what the problem was. I'm confused as to why you chose to
disregard it and instead change the syntax of your module call
syntax...

Paul Lalli


Similar ThreadsPosted
Lower case module name for non-pragma module January 4, 2005, 10:19 am
RFC: New module 'Module::Bundled::Files' August 26, 2005, 3:49 pm
help with an MD5.pm module!! September 3, 2005, 11:39 pm
Looking for RTP module December 9, 2004, 9:17 pm
module for FFT May 9, 2005, 3:06 pm
Module may not be right one? April 27, 2006, 12:57 pm
The 'if' module June 1, 2006, 8:26 pm
use module March 3, 2007, 5:13 am
Name my Module! July 27, 2007, 6:46 pm
Yet another TL1 module August 7, 2007, 4:12 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap