简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM