The "if" module
am 02.06.2006 02:18:41 von Sisyphus
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
Re: The "if" module
am 02.06.2006 03:30:21 von Gunnar Hjalmarsson
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
Re: The "if" module
am 02.06.2006 03:55:30 von Paul Lalli
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
Re: The "if" module
am 02.06.2006 04:39:09 von Sisyphus
"Paul Lalli" wrote in message
news:1149213330.151488.24350@c74g2000cwc.googlegroups.com...
> 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
Re: The "if" module
am 02.06.2006 13:52:23 von Paul Lalli
Sisyphus wrote:
> "Paul Lalli" wrote in message
> news:1149213330.151488.24350@c74g2000cwc.googlegroups.com...
> > 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
Re: The "if" module
am 03.06.2006 04:19:53 von Sisyphus
"Paul Lalli" wrote in message
news:1149249143.316925.80170@f6g2000cwb.googlegroups.com...
> Sisyphus wrote:
> > "Paul Lalli" wrote in message
> > news:1149213330.151488.24350@c74g2000cwc.googlegroups.com...
> > > 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 don't think this is the forum to discuss your needs.
I'm also not so sure that you and I would agree on the meaning of "exactly".
The term "some code" does not strike me as being all that exact.
> I'm confused as to why you chose to
> disregard it and instead change the syntax of your module call
> syntax...
>
I "chose to disregard it" ??
The arrogance of that assertion is, of course, insulting. The fact that it's
false doesn't really bother me.
Cheers,
Rob
Re: The "if" module
am 03.06.2006 13:35:51 von Paul Lalli
Sisyphus wrote:
> "Paul Lalli" wrote in message
> news:1149249143.316925.80170@f6g2000cwb.googlegroups.com...
> > Sisyphus wrote:
> > > "Paul Lalli" wrote in message
> > > news:1149213330.151488.24350@c74g2000cwc.googlegroups.com...
> > > > 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 don't think this is the forum to discuss your needs.
> I'm also not so sure that you and I would agree on the meaning of "exactly".
> The term "some code" does not strike me as being all that exact.
'some code' on line 2, which was:
use if $^O =~ /another/, "Non::Existent";
Exactly how many parts of that code do you think can return an empty
list in list context?
> > I'm confused as to why you chose to
> > disregard it and instead change the syntax of your module call
> > syntax...
> >
>
> I "chose to disregard it" ??
Sure seems that way to me. Your original message to this newsgroup
contained a piece of code that was presumably your latest attempt. It
was counter to the way the module's call syntax is supposed to be
given. It bared no resemblence to any fix suggested by the error
message. Further, your original post made absolutely no mention of the
original attempt, or of the error message that you later told us it
gave you.
> The arrogance of that assertion is
It's an assumption, based on evidence presented by your posts. Not an
assertion.
>, of course, insulting.
I'm sorry you felt insulted.
> The fact that it's false doesn't really bother me.
Okay. Please explain to me what you did when you saw that error
message. Because your apparent next attempt at the module call
certainly had nothing to do with anything the error message told you.
I'm really not interested in getting into an argument about this, so
this will be my last message in this thread. You're welcome to have
the last word.
Paul Lalli
Re: The "if" module
am 04.06.2006 13:22:55 von Sisyphus
"Paul Lalli" wrote in message
> >
> > I "chose to disregard it" ??
>
> It's an assumption, based on evidence presented by your posts. Not an
> assertion.
Yeah - but I read that as an unqualified assertion rather than an assumption
(and I still do read it that way) - and that's what really irked me. Still -
I shouldn't have snapped - and I apologise for so doing.
>
> Okay. Please explain to me what you did when you saw that error
> message. Because your apparent next attempt at the module call
> certainly had nothing to do with anything the error message told you.
>
Well - you'll note that "my next attempt at the module call" added an extra
argument - which *did* have something to do with what the error message told
me. Not only that, but it worked to the extent that the script compiled
without any warnings being issued, and behaved as expected - iff CONDITION
was true. I think I was also sucked in by my own (false) assumption that I
knew what "use if CONDITION" (from the docs) meant. I was therefore lead to
concentrate on the "MODULE => ARGUMENTS" bit (which I wasn't sure about).
Beyond that I don't see much point in providing details on my (flawed)
thought processes.
Anyway - thanks again for the original explanation. The elaboration I
provided in my follow-up was meant simply to demonstrate that I understood
the points you had made.
Cheers,
Rob
Re: The "if" module
am 05.06.2006 07:30:03 von Ilya Zakharevich
[A complimentary Cc of this posting was sent to
Sisyphus
], who wrote in article <4482c365$0$16769$afc38c87@news.optusnet.com.au>:
> > Okay. Please explain to me what you did when you saw that error
> > message. Because your apparent next attempt at the module call
> > certainly had nothing to do with anything the error message told you.
> >
>
> Well - you'll note that "my next attempt at the module call" added an extra
> argument - which *did* have something to do with what the error message told
> me. Not only that, but it worked to the extent that the script compiled
> without any warnings being issued, and behaved as expected - iff CONDITION
> was true. I think I was also sucked in by my own (false) assumption that I
> knew what "use if CONDITION" (from the docs) meant. I was therefore lead to
> concentrate on the "MODULE => ARGUMENTS" bit (which I wasn't sure about).
>
> Beyond that I don't see much point in providing details on my (flawed)
> thought processes.
I do. A little bit of history: when I designed if.pm, I had no the
idea that CONDITION is subject to cryptocontext. When somebody
reported this behaviour as a bug, the best "solution" I got was to try
to detect this situation (only possible when module is called without
arguments!), and provide a readable error message.
The current error message may be improved a lot. I welcome everybody
to think the message over and try to make it more clear. Likewise,
for
use if CONDITION, strict => 'refs';
with CONDITION returning en empty list, what the module sees is
CONDITION := 'strict', MODULE := 'refs'. Since there is no refs.pm,
the if.pm could also detect this, and emit a similar message.
Patches welcome,
Ilya