如何获取当前-运行 Perl 脚本的名称?
How do I get the name of the currently-running Perl script?
我有剧本
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':default';
use mwe 'tmp';
tmp();
调用 Perl 模块 mwe
use feature 'say';
package mwe;
use Cwd 'getcwd';
use Exporter qw(import);
our @EXPORT = qw(tmp);
sub tmp {
say 'written by ' . getcwd() . '/' . __FILE__;
}
1;
但是当我 运行 这个脚本时,文件名出现在模块中:
con@V:~/Scripts$ perl mwe.pl
written by /home/con/Scripts//home/con/perl5/perlbrew/perls/perl-5.34.0/lib/5.34.0/mwe.pm
我在编写模块方面还是个新手,所以如果我的最小工作示例写得不好,欢迎批评。
我的问题:
我知道我可以将文件 /home/con/Scripts/mwe.pl
作为参数传递给子例程 tmp
,但是有没有一种方法可以让子例程像 tmp
到 return脚本文件名改为自动?
而不是 mwe.pm 中的这个:
say 'written by ' . getcwd() . '/' . __FILE__;
使用:
say 'written by ' . getcwd() . '/' . [=11=];
请调查以下代码片段是否符合您的问题。
在主脚本中使用 our $__SCRIPT__ = abs_path([=13=])
声明的变量采用脚本名称 [=14=]
并添加绝对路径。
可以从任何模块访问此变量,如 $main::__SCRIPT__
。
use strict;
use warnings;
use feature 'say';
use Cwd 'abs_path';
use Test::Some;
our $__SCRIPT__ = abs_path([=10=]);
my $args = {
id => '2000',
str => 'Magic string'
};
my $m = new Some($args);
$m->show();
模块源代码Test::Some
package Some;
use strict;
use warnings;
use feature 'say';
sub new {
my ($class, $arg) = @_;
my $self = bless {
id => $arg->{id},
str => $arg->{str}
}, $class;
return $self;
}
sub show {
my $self = shift;
say 'PACKAGE: ' . __PACKAGE__;
say 'PKG_FILE: ' . __FILE__;
say 'SCRIPT: ' . $main::__SCRIPT__;
say 'SELF_ID: ' . $self->{id};
say 'SELF_STR: ' . $self->{str};
}
1;
输出样本
PACKAGE: Some
PKG_FILE: /kunden/homepages/6/d807xxxxxx/htdocs/.perl/Test/Some.pm
SCRIPT: /homepages/6/d807xxxxxx/htdocs/work/perl/examples/xmodule.pl
SELF_ID: 2000
SELF_STR: Magic string
参考:
abs_path,
Perl global variables
使用
use FindBin qw( $RealScript );
say $RealScript;
或
use Cwd qw( abs_path );
say abs_path([=11=]);
大多数时候,人们实际上想要脚本所在的目录。为此,使用
use FindBin qw( $RealBin );
say $RealBin;
或
use Cwd qw( abs_path );
use File::Basename qw( dirname );
say dirname(abs_path([=13=]));
我有剧本
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':default';
use mwe 'tmp';
tmp();
调用 Perl 模块 mwe
use feature 'say';
package mwe;
use Cwd 'getcwd';
use Exporter qw(import);
our @EXPORT = qw(tmp);
sub tmp {
say 'written by ' . getcwd() . '/' . __FILE__;
}
1;
但是当我 运行 这个脚本时,文件名出现在模块中:
con@V:~/Scripts$ perl mwe.pl
written by /home/con/Scripts//home/con/perl5/perlbrew/perls/perl-5.34.0/lib/5.34.0/mwe.pm
我在编写模块方面还是个新手,所以如果我的最小工作示例写得不好,欢迎批评。
我的问题:
我知道我可以将文件 /home/con/Scripts/mwe.pl
作为参数传递给子例程 tmp
,但是有没有一种方法可以让子例程像 tmp
到 return脚本文件名改为自动?
而不是 mwe.pm 中的这个:
say 'written by ' . getcwd() . '/' . __FILE__;
使用:
say 'written by ' . getcwd() . '/' . [=11=];
请调查以下代码片段是否符合您的问题。
在主脚本中使用 our $__SCRIPT__ = abs_path([=13=])
声明的变量采用脚本名称 [=14=]
并添加绝对路径。
可以从任何模块访问此变量,如 $main::__SCRIPT__
。
use strict;
use warnings;
use feature 'say';
use Cwd 'abs_path';
use Test::Some;
our $__SCRIPT__ = abs_path([=10=]);
my $args = {
id => '2000',
str => 'Magic string'
};
my $m = new Some($args);
$m->show();
模块源代码Test::Some
package Some;
use strict;
use warnings;
use feature 'say';
sub new {
my ($class, $arg) = @_;
my $self = bless {
id => $arg->{id},
str => $arg->{str}
}, $class;
return $self;
}
sub show {
my $self = shift;
say 'PACKAGE: ' . __PACKAGE__;
say 'PKG_FILE: ' . __FILE__;
say 'SCRIPT: ' . $main::__SCRIPT__;
say 'SELF_ID: ' . $self->{id};
say 'SELF_STR: ' . $self->{str};
}
1;
输出样本
PACKAGE: Some
PKG_FILE: /kunden/homepages/6/d807xxxxxx/htdocs/.perl/Test/Some.pm
SCRIPT: /homepages/6/d807xxxxxx/htdocs/work/perl/examples/xmodule.pl
SELF_ID: 2000
SELF_STR: Magic string
参考: abs_path, Perl global variables
使用
use FindBin qw( $RealScript );
say $RealScript;
或
use Cwd qw( abs_path );
say abs_path([=11=]);
大多数时候,人们实际上想要脚本所在的目录。为此,使用
use FindBin qw( $RealBin );
say $RealBin;
或
use Cwd qw( abs_path );
use File::Basename qw( dirname );
say dirname(abs_path([=13=]));