Perl getcwd 结束正斜杠

发布于 2024-07-22 11:44:43 字数 915 浏览 5 评论 0原文

我正在编写一个 Perl 脚本来将另一个变量附加到当前工作目录的末尾,但我在使用该模块时遇到问题。

  1. 如果我从 D:\ 运行 getcwd,返回的值为

    D:/(带正斜杠)   
      

    如果我从 D:\Temp\ 运行 getcwd,返回的值为

    D:/temp(不带正斜杠) 
      

    这使得情况变得非常棘手,因为如果我简单地这样做:

    使用Cwd; 
      $ProjectName = "项目";   # 这是用户提供的变量 
      $directory = getcwd().$ProjectName."\/"; 
      打印$目录。"\n"; 
      

    我最终会得到其中一个

    D:/Project(正确)   
      

    D:/TempProject(而不是 D:/Temp/Project) 
      

    这是 Cwd 中的功能吗? 文档中似乎没有。

  2. 我想出了以下代码来解决这个问题。 需要3行才能完成。 你们谁能想到更简洁的方法吗?

    使用Cwd; 
      $ProjectName = "项目";   # 这是用户提供的变量 
    
      $目录= getcwd(); 
      $目录 =~ s/(.+?)([^\\\/])$/$1$2\//g;   # 如果不以正斜杠/反斜杠结尾则附加“/” 
      $directory.= $ProjectName."\/"; 
    
      打印$目录。"\n"; 
      

I am doing a Perl script to attach another variable to the end of the current working directory, but I am having problems with using the module.

  1. If I run getcwd from D:\, the value returned is

    D:/ (with forward slash)  
    

    If I run getcwd from D:\Temp\, the value returned is

    D:/temp (without forward slash)
    

    This makes the situation quite tricky because if I simply do:

    use Cwd;
    $ProjectName = "Project"; # This is a variable supplied by the user
    $directory = getcwd().$ProjectName."\/";
    print $directory."\n";
    

    I will end up with either

    D:/Project (correct)  
    

    or

    D:/TempProject (instead of D:/Temp/Project)
    

    Is this a feature in Cwd? It does not seem to be in the documentation.

  2. I have thought up the following code to solve this issue. It takes 3 lines to do it. Can any of you see a more concise way?

    use Cwd;
    $ProjectName = "Project"; # This is a variable supplied by the user
    
    $directory = getcwd();
    $directory =~ s/(.+?)([^\\\/])$/$1$2\//g;  # Append "/" if not terminating with forward/back slash
    $directory .= $ProjectName."\/";
    
    print $directory."\n";
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-07-29 11:44:43

使用 File::Spec 而不是创建自己的路径操作例程。

use Cwd;
use File::Spec;

$ProjectName = "Project";

$cwd = getcwd();
$directory = File::Spec->catdir($cwd, $ProjectName);

print "$directory\n";

Use File::Spec instead of making your own path manipulation routines.

use Cwd;
use File::Spec;

$ProjectName = "Project";

$cwd = getcwd();
$directory = File::Spec->catdir($cwd, $ProjectName);

print "$directory\n";
停滞 2024-07-29 11:44:43

第一种情况是包含尾部斜杠,因为“D:”是卷说明符。 它不是有效的目录名称。 “D:/”类似于 Unix/Linux 中的根目录。

快速而肮脏的解决方案:

$directory .= '/' unless $directory =~ m|/$|;

要获得强大且可移植的解决方案,请使用 File::SpecFile::Spec::Functions 如果您更喜欢非面向对象接口:

use Cwd;
use File::Spec::Functions qw(catdir);

my $directory = catdir(getcwd(), $ProjectName);

请注意,catdir 不包含尾部斜杠,并且 File::Spec 使用主机操作系统的目录分隔符构建路径。 (例如 Windows 上的反斜杠)。

The first case is including the trailing slash because "D:" is a volume specifier. It isn't a valid directory name. "D:/" is analogous to the root directory in Unix/Linux.

Quick and dirty solution:

$directory .= '/' unless $directory =~ m|/$|;

For a robust and portable solution, use File::Spec or File::Spec::Functions if you prefer a non-object-oriented interface:

use Cwd;
use File::Spec::Functions qw(catdir);

my $directory = catdir(getcwd(), $ProjectName);

Note that catdir does not include the trailing slash, and that File::Spec builds paths using the directory separator for the host operating system. (e.g. backslashes on Windows).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文