2013-07-21 24 views
4

在OS X 10.8.4,在測試Perl程序:爲什麼Perl不能找到File :: BaseName-> fileparse?

#!/usr/bin/perl 

use warnings; 
use strict; 
use File::BaseName; 

my $fname = "/usr/local/junk.txt"; 
my ($name, $path, $suffix1) = File::BaseName->fileparse($fname, qr'\.[^\.]*'); 

任何想法,爲什麼我得到的錯誤信息:

Can't locate object method "fileparse" via package "File::BaseName" 
(perhaps you forgot to load "File::BaseName"?) 

對於這個問題,爲什麼我需要把File::BaseName?如果我不這樣做,它說

Undefined subroutine &main::fileparse 

的perl -v給出:

這是 達爾文線程多建的Perl 5,第12版,顛覆4(v5.12.4) 2level

而且@INC包括/System/Library/Perl/5.12/和 /System/Library/Perl/5.12/File/BaseName.pm存在,並在 已fileparse它。

萬一有幫助,當我使用File::Spec,並參考File::Spec->splitpath,工作正常(但我必須把全行)。

+0

的[Perl模塊,方法調用可能重複:不能調用方法「X」上爲$未定義的值{SOMEFILE}線$ {SOMELINE}](http://stackoverflow.com/questions/3597605/perl-module-method-calls-cant-call-method-x-on-an-undefined- value-at-somef) – Barmar

+0

PBP:對正則表達式使用斜槓或大括號:'qr {\。 [^ \。] * \ z} msx' – shawnhcorey

+0

這不是Barmar鏈接到的問題的重複內容,請不要投票結束。 – daxim

回答

8

區分大小寫: Basename用小寫字母「N」寫成。 Acme::require::case將防止這個問題。

此外,您不必使用限定名稱fileparse導入了File::Basename模塊後:

#!/usr/bin/perl 

use warnings; 
use strict; 
use File::Basename; # !!! 

my $fname = "/usr/local/junk.txt"; 
my ($name, $path, $suffix1) = fileparse($fname, qr'\.[^\.]*'); # !!! 
+3

他也沒有爲合格的名稱使用正確的語法。它是'::',而不是' - >'。 – Barmar

+0

@Barmar:是的,我錯過了,thx! –

+0

就是這樣。謝謝。能夠凝視某事而看不到它的時間是多麼的驚人。作爲一名perl新手,如果沒有找到該文件,他會認爲它會在使用聲明中抱怨。在開發過程中,Acme參考也非常有用。 – mackworth

相關問題