簡體   English   中英

如何在Perl中的路徑中獲取目錄

[英]How to get the directory in a path in perl

我有一個目錄路徑:

my $temp = "/a/b/c/d";
my $upDirectory = dirname( $temp );

這將返回“ / a / b / c”。 但是我只需要“ c”。 我必須將其與字符串進行比較,以確定它是否是正確的目錄。

除了在“ /”上分割以外,還有其他方法嗎? 我不想使用這個。 Perl代碼必須在多個平台上使用。 可以幫助splitdir嗎?

當然,有很多方法可以實現,但是如果您主要關注的是到其他平台的可移植性,那么File :: Spec可能是一個合理的選擇。 尋找splitpathsplitdir

強制Path::Class解決方案:

use Path::Class qw(dir);
dir('/a/b/c/d')->parent->dir_list(-1)
# expression returns 'c'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM