[comp.lang.perl] nested requires, name-space override

rampson@flash (Michael T. Rampson) (05/23/91)

Howdy,
	I was wondering if you are suppose to be able to do nested requires.
I haven't been able to get them to work ("I get Can't locate dodat.pl in @INC 
at do_this.pl line 1.").  What I'm trying to do is create an abstract interface
of things you have to do to handle different types of program builders
(ie., make, gmake, imake, mkmf, etc).  Originally I found that there was
some invariant code (ie., code that did not depend on what type of make you
were using), and some code that was very specific to the MAKER that you
were using.  But after thinking about it, what I would really like to do
is be able to override a function defined in the default library (default
being things to do with make), with a function that is specific to the MAKER
needed (that would be in another library).  The reason I'm splitting this stuff
out is so that you don't end up basically the same code replicated all over the
place.  Below is an example of what I would like to be able to do.

require 'maker.pl';
&make(blah, blah, target) || die "a horrible death:$!";

maker.pl would look like this
package maker;
eval require $ENV{'MAKER'}.pl;
sub main'make {
	local(blah,blah,$target)=@_;
	'make $target';
	if ($?) {
		warn "make died:$!";
		return(0);
	}
	return(1);
}
1;

an example MAKER.pl would look something like the above only redefine
main'make to do whatever is needed for this particular kind of MAKER.

Any ideas?

__
The objective of all dedicated employees should be to thoroughly
analyze all situations, anticipate all problems prior to their
occurrence, have answers for these problems, and move swiftly to solve
these problems when called upon.

However, When you are up to your ass in alligators it is difficult to
remind yourself your initial objective was to drain the swamp.

    	    	    	    	    	    rampson@uswat

rampson@flash (Michael T. Rampson) (05/23/91)

Aaaaaaaaaacccccccchhhhhhhhh!!!!  Never mind!!  Pilot error.  Anyway, for
anyone interested, this does work just the way I want it to (especially if you
name the library file correctly |-( ...sigh).  You can have multiple includes
and you can override functions (the function at the bottom of the requires
will override any up level definitions, so it doesn't seem to matter when
you do the require (ie., at the beginning of the library or at the end)).

			Sorry for the wasted BW,
			mike
__
The objective of all dedicated employees should be to thoroughly
analyze all situations, anticipate all problems prior to their
occurrence, have answers for these problems, and move swiftly to solve
these problems when called upon.

However, When you are up to your ass in alligators it is difficult to
remind yourself your initial objective was to drain the swamp.

    	    	    	    rampson@uswat.uswest.com

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/23/91)

In article <1991May22.193037.12166@cherokee.uswest.com> rampson@flash (Michael T. Rampson) writes:
: Howdy,
: 	I was wondering if you are suppose to be able to do nested requires.

Yes.

: I haven't been able to get them to work ("I get Can't locate dodat.pl in @INC 
: at do_this.pl line 1.").  What I'm trying to do is create an abstract interface
: of things you have to do to handle different types of program builders
: (ie., make, gmake, imake, mkmf, etc).  Originally I found that there was
: some invariant code (ie., code that did not depend on what type of make you
: were using), and some code that was very specific to the MAKER that you
: were using.  But after thinking about it, what I would really like to do
: is be able to override a function defined in the default library (default
: being things to do with make), with a function that is specific to the MAKER
: needed (that would be in another library).  The reason I'm splitting this stuff
: out is so that you don't end up basically the same code replicated all over the
: place.  Below is an example of what I would like to be able to do.
: 
: require 'maker.pl';
: &make(blah, blah, target) || die "a horrible death:$!";
: 
: maker.pl would look like this
: package maker;
: eval require $ENV{'MAKER'}.pl;

That won't do what you think it does.  You want something like

	eval q/require "$ENV{'MAKER'}.pl"/;
	warn $@ if $@;

You can also get finer control by using "do" instead of "require".
It won't keep track of what you've already done, but you don't have
to use eval to trap a bad return from it.

: sub main'make {
: 	local(blah,blah,$target)=@_;
: 	'make $target';
: 	if ($?) {
: 		warn "make died:$!";
: 		return(0);
: 	}
: 	return(1);
: }
: 1;
: 
: an example MAKER.pl would look something like the above only redefine
: main'make to do whatever is needed for this particular kind of MAKER.

That should be possible, since the require will happen after the compilation
of the current main'make, so any main'make defined by the require should
override the current one.

Incidentally, I hope it's just a typo that you said 'make $target' using
single quotes instead of backticks.  Also, just because $? is set doesn't
mean that $! will have a meaningful value.

Larry