<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<font size="+1"><tt>put this in MyModule.pm:<br>
<br>
package MyModule;<br>
use strict;<br>
use warnings;<br>
our $database;<br>
<br>
sub import {<br>
&nbsp; my $class = shift;<br>
&nbsp; for (my $i=0; $i&lt; $#_; $i+=2) {<br>
&nbsp;&nbsp;&nbsp; if ($_[$i] eq "database") {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $database = $_[$i+1];<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp; }<br>
&nbsp; if ($database) {<br>
&nbsp;&nbsp;&nbsp;&nbsp; print "Using database: $database $/";<br>
&nbsp; }<br>
&nbsp; return $class;<br>
}<br>
<br>
sub do_something {<br>
&nbsp; print "do_something(".join(",<a class="moz-txt-link-rfc2396E" href="mailto:,@_).">",@_)."</a> with database $database $/";<br>
}<br>
<br>
sub DESTROY {<br>
&nbsp; if ($database) {<br>
&nbsp;&nbsp;&nbsp; print "Disconnecting database: $database $/";<br>
&nbsp; }<br>
}<br>
<br>
1;<br>
<br>
<br>
Now this in test.pl<br>
<br>
</tt></font><tt><font size="+1">#!/usr/bin/perl<br>
<br>
use strict;<br>
use warnings;<br>
use MyModule database =&gt; "fred";<br>
<br>
print "main $/";<br>
MyModule::do_something("SELECTING...");<br>
</font></tt><br>
<font size="+1"><tt><br>
now:<br>
<br>
set PERL5LIB="$PERL5LIB:."<br>
chmod +x test.pl<br>
./test.pl<br>
<br>
<br>
<br>
So.... basically create a sub called "import" - the first argument is
the package/class name.&nbsp; The rest of the arguments are whatever you
supply as arguments to the 'use'.&nbsp; Parse the args and store your value
in an 'our' variable.<br>
<br>
This help?<br>
Mathew Robertson<br>
<br>
<br>
<br>
</tt></font><br>
Josh Heumann wrote:
<blockquote cite="mid20070523043435.GE16957@joshheumann.com" type="cite">
  <pre wrap="">Hi, all.  After some confusing book- and soul-searching, I come to the
group with a question.  I want to do something like this:

use MyModule env =&gt; test;

to tell the module that is being used that it should use the test
database.  What do I need to do to capture that in MyModule?

For extra credit, what if I want to use that information in something
in MyModule that happens in a statement that isn't in a subroutine?

All of my tests seem to indicate that I'm out of luck because the
statement outside of a subroutine gets executed at compile time, while
the use statement gets run at runtime.  I feel like I could cobble
something together, but it would be a hack and just plain wrong.

Thanks for your help.

Josh
_______________________________________________
Melbourne-pm mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Melbourne-pm@pm.org">Melbourne-pm@pm.org</a>
<a class="moz-txt-link-freetext" href="http://mail.pm.org/mailman/listinfo/melbourne-pm">http://mail.pm.org/mailman/listinfo/melbourne-pm</a>
  </pre>
</blockquote>
</body>
</html>