4

On OS X 10.8.4, in a test perl program:

#!/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'\.[^\.]*');

Any ideas why I get the error message:

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

For that matter, why do I need to put File::BaseName? If I don't, it says

Undefined subroutine &main::fileparse

perl -v gives:

This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level

And @INC includes /System/Library/Perl/5.12/ and /System/Library/Perl/5.12/File/BaseName.pm exists and has fileparse in it.

In case it helps, when I Use File::Spec and refer to File::Spec->splitpath, that works fine (but I do have to put the full line).

TLP
  • 66,756
  • 10
  • 92
  • 149
mackworth
  • 5,873
  • 2
  • 29
  • 49
  • possible duplicate of [Perl Module Method Calls: Can't call method "X" on an undefined value at ${SOMEFILE} line ${SOMELINE}](http://stackoverflow.com/questions/3597605/perl-module-method-calls-cant-call-method-x-on-an-undefined-value-at-somef) – Barmar Jul 21 '13 at 08:41
  • PBP: use slashes or curly braces for regex: `qr{ \. [^\.]* \z }msx` – shawnhcorey Jul 21 '13 at 13:02
  • It's not the duplicate of the problem Barmar linked to, don't vote to close. – daxim Jul 21 '13 at 13:58

1 Answers1

8

It's case sensitivity: Basename is written with lowercase letter "N". Acme::require::case will prevent that problem.

Besides, you don't have to use a qualified name for fileparse after you've imported the File::Basename module:

#!/usr/bin/perl

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

my $fname = "/usr/local/junk.txt";
my ($name, $path, $suffix1) = fileparse($fname, qr'\.[^\.]*'); # !!!
daxim
  • 39,270
  • 4
  • 65
  • 132
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 3
    he also wasn't using the correct syntax for the qualified name. It's `::`, not `->`. – Barmar Jul 21 '13 at 08:46
  • That was exactly it. Thank you. It's amazing how long one can stare at something and not see it. Being a perl novice, on would think it would have complained on the use statement when it didn't find the file. The Acme reference is quite useful also to prevent this during development. – mackworth Jul 21 '13 at 17:10
  • @mackworth: "how long one can stare at something and not see it" - I think everyone experienced this phenomenon - you can't find a simple mistake in your own code, while you would easily see the same mistake in someone else's code. A possible reason is that you know your own code too good and don't read it very carefully... You are welcome! – Alex Shesterov Jul 21 '13 at 17:48