[Chicago-talk] Where'd I come from?

Jonathan Rockway jon at jrock.us
Fri Jan 26 09:36:35 PST 2007


Andy_Bach at wiwb.uscourts.gov wrote:
> Hey,
>
> Somebody was asking me about code like:
> eval("require '../support/this.pl' ");  # has sub foo
> ...
> eval("foo()");

Why are you using string evals?  What's wrong with:

eval {
  require 'whatever';
  foo();
}


> That is, sub name and main script, but not the 'require'-ed script name.
> Is there a way to get that?

However, I'm pretty sure requiring a .pl file dumps everything into
main, which is why you don't get the script name that foo() is from
(perl can't tell you because it doesn't know anymore).

How about:

package ThatOtherScript;
do 'whatever.pl';

package main;
ThatOtherScript::foo();

Untested, but might work.  The way to get this working reliably is to
just make a module and use it:

in ThatScript.pm:

  package ThatScript;
  use base 'Exporter';
  our @EXPORT_OK = qw(foo);
  out @EXPORT = qw(foo);
  sub foo { print 'foo was called' }
  1;

in main.pl:
  use FindBin ($Bin);
  use lib "$Bin/.."; # or wherever
  use ThatScript;

  foo(); # prints 'foo was called';

Much easier than trying to hack together your own code loading system :)

-- 
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)->config(name => do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
";$;"]->[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;->setup;


More information about the Chicago-talk mailing list