How do I get the full path to a Perl script that is executing?

后端 未结 23 2952
情深已故
情深已故 2020-11-28 19:29

I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0 va

23条回答
  •  醉梦人生
    2020-11-28 19:55

    Without any external modules, valid for shell, works well even with '../':

    my $self = `pwd`;
    chomp $self;
    $self .='/'.$1 if $0 =~/([^\/]*)$/; #keep the filename only
    print "self=$self\n";
    

    test:

    $ /my/temp/Host$ perl ./host-mod.pl 
    self=/my/temp/Host/host-mod.pl
    
    $ /my/temp/Host$ ./host-mod.pl 
    self=/my/temp/Host/host-mod.pl
    
    $ /my/temp/Host$ ../Host/./host-mod.pl 
    self=/my/temp/Host/host-mod.pl
    

提交回复
热议问题