tags:

views:

60

answers:

1

Is there anyway to tell from inside a module's import {}

perl -MFoo -e1

apart from

perl -e'use Foo;'

and, likewise

perl -e'package main; use Foo;'

I'm trying to have two distinct behaviors for these two. In the -MFoo syntax, I want the behavoir of oose.pm, but I don't want to have import called in the main namespace. In the other syntaxes, I want the sub import to happily occur.

+4  A: 

Is the call stack different in each of those cases? It might be as simple at peeking at caller(0).

Looks like the 'line' component is different, at least between cases #1 and #2:

package Foo;
use strict;
use warnings;
use Data::Dumper;
sub import
{
    print Dumper([caller(0)]);
}
1;

# perl -MFoo -e1
$VAR1 = [
          'main',
          '-e',
          0,
          'Foo::import',
          1,
          0,
          undef,
          undef,
          0,
          ''
        ];

# perl -e'use Foo'
$VAR1 = [
          'main',
          '-e',
          1,
          'Foo::import',
          1,
          0,
          undef,
          undef,
          0,
          ''
        ];

# perl -e'package main; use Foo;'
$VAR1 = [
          'main',
          '-e',
          1,
          'Foo::import',
          1,
          0,
          undef,
          undef,
          0,
          ''
        ];
Ether