开发者

How do I know if my Perl module is included under -M?

开发者 https://www.devze.com 2023-01-19 11:41 出处:网络
Is there anyway to tell from inside a module\'s import {} perl -MFoo -e1 apart from perl -e\'use Foo;\' and, likewise

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.


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,
          ''
        ];
0

精彩评论

暂无评论...
验证码 换一张
取 消