2011-12-08 34 views

回答

7

使用File::Basename

> perl -MFile::Basename -wE "say dirname(qq(c:/perl/bin/perl.exe));" 
c:/perl/bin 
+2

文件::規格是另一種選擇。 (幾年前,我從File :: Basename切換到File :: Spec;如果我記得爲什麼我這麼做,我會跟進)。 –

+0

@KeithThompson'注意:File :: Basename的dirname()和basename()模擬同名的shell和C函數的行爲和怪癖。如果你的問題只是解析路徑,使用File :: Spec的splitpath()和splitdir()方法更安全。# – unixman83

5

您還可以使用Path::Class

#!/usr/bin/env perl 

use strict; 
use warnings; 

use Path::Class; 

my $file = file('c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do'); 

print $file->parent, "\n"; 

輸出:

C:\My_Designs\ipcore_script\test\src\IP_CORE\mux
+1

嗨,感謝您的解決方案,但我得到這個錯誤。無法找到@INC中的Path/Class.pm(@INC包含:C:/ Perl64/site/lib C:/ Perl64/ lib。)at script.pl line 6. BEGIN失敗 - 在腳本中編譯異常.pl第6行。在哪裏可以找到這個class.pm庫 – mitt2050

+1

@ mitt2050他的答案中有一個鏈接。但是,如果您使用ActivePerl,則可以使用其包管理器。在cmd提示符下,只需輸入'ppm install Path :: Class'。或者只是'ppm'來訪問GUI。 – TLP

0

你也可以試試這個

$path='c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do'; 

my $pathname=substr($path, 0, rindex($path,"\\")); 

Result: 
c:\My_Designs\ipcore_script\test\src\IP_CORE\mux 
相關問題