On 3/5/06, Chris Dolan <[EMAIL PROTECTED]> wrote:
> On Mar 5, 2006, at 3:55 PM, David Wheeler wrote:
>
> > On Mar 5, 2006, at 13:52, Chris Dolan wrote:
> >
> >> Advice?  While this example is contrived, the "eval
> >> { require ... }" idiom is used often in the wild, so this is not a
> >> wholly unrealistic scenario.
> >
> > Of course it should be
> >
> >   eval { require Bar; 1; } or die $@;
> >
> > But I agree that it seems like if the load failed in your eval, it
> > should fail for Test::More, too. But maybe even a failed require
> > sets the record in %INC?
>
> In this case, Bar.pm is intended to represent optional functionality
> that Foo.pm conditionally loads.  So, adding a die would be
> counterproductive.  The problem is that I don't know how to
> distinguish between a load failure or a compile failure.  There must
> be a way to do that right?

The way to do it right is to run Perl's module finding subroutine and
if it finds the requested module then you can require it without an
eval (or just do "path/to/module"), if it doesn't then you skip it.

Sadly Perl does not give access to the module finding routine. I have
a vague idea that there's something on CPAN that implements it
(perldoc -f require shows some example code) but that won't handle all
the tricks you can do wth objects etc in @INC.

Another option is to eval and analyse $@ afterwards, maybe use a regex
to see it contains an error about "Can't locate Foo/Bar.pm".

Last option that I can think of is do the eval {require } and if it
fails, check %INC. If the module isn't there then you you know it
failed because it wasn't found. If the module is there then you know
it was found but died for some other reason so you should rethrow the
error with die [EMAIL PROTECTED]

I think the latter is probably the most reliable as hitting $@ with
regexes could lead to nasty surprises,

F

>
> Chris
> --
> Chris Dolan, Software Developer, Clotho Advanced Media Inc.
> 608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703
> vCard: http://www.chrisdolan.net/ChrisDolan.vcf
>
> Clotho Advanced Media, Inc. - Creators of MediaLandscape Software
> (http://www.media-landscape.com/) and partners in the revolutionary
> Croquet project (http://www.opencroquet.org/)
>
>
>

Reply via email to