【问题标题】:How do I get the name of the currently-running Perl script?如何获取当前运行的 Perl 脚本的名称?
【发布时间】:2021-06-02 20:38:03
【问题描述】:

我有一个脚本

#!/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 这样的子程序自动返回脚本文件名?

【问题讨论】:

    标签: perl


    【解决方案1】:

    使用

    use FindBin qw( $RealScript );
    say $RealScript;
    

    use Cwd qw( abs_path );
    say abs_path($0);
    

    大多数时候,人们实际上想要脚本所在的目录。为此,请使用

    use FindBin qw( $RealBin );
    say $RealBin;
    

    use Cwd qw( abs_path );
    use File::Basename qw( dirname );
    say dirname(abs_path($0));
    

    【讨论】:

      【解决方案2】:

      而不是在 mwe.pm 中:

      say 'written by ' . getcwd() . '/' . __FILE__;
      

      用途:

      say 'written by ' . getcwd() . '/' . $0;
      

      【讨论】:

      • 这通常会失败。
      • 需要明确的是,如果将绝对路径传递给perl,这将失败,这很常见。
      • @ikegami 一开始我差点给出一个响应,将文件路径从脚本传递到模块。
      【解决方案3】:

      如果符合您的问题,请调查以下代码sn-p。

      在主脚本中使用our $__SCRIPT__ = abs_path($0) 声明的变量,它采用脚本名称$0 并添加绝对路径。

      可以从任何模块以$main::__SCRIPT__ 访问此变量。

      use strict;
      use warnings;
      use feature 'say';
      
      use Cwd 'abs_path';
      use Test::Some;
      
      our $__SCRIPT__ = abs_path($0);
      
      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

      【讨论】:

        猜你喜欢
        • 2011-06-03
        • 2017-09-09
        • 2011-05-08
        • 2010-11-25
        • 2014-05-03
        • 1970-01-01
        • 2010-11-29
        • 2018-01-14
        • 2017-08-04
        相关资源
        最近更新 更多