Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Turning pathname into package name in a portable way

by itub (Priest)
on Oct 15, 2005 at 19:47 UTC ( [id://500492]=perlquestion: print w/replies, xml ) Need Help??

itub has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, when I want to turn a Unix pathname into a package name, I usually do something like this:

my $package = $pathname; $package =~ s/\//::/g; $package =~ s/\.\w+$//;

and it works for me. But what if I want to make sure it's really portable? After reading perlport, I feel intimidated, but I'm sure that someone has faced this problem before. Any suggestions?

Replies are listed 'Best First'.
Re: Turning pathname into package name in a portable way
by idsfa (Vicar) on Oct 15, 2005 at 20:46 UTC
    use File::Spec; my ($volume, $dirstring, $file) = File::Spec->splitpath($pathname); my @dirs = File::Spec->splitdir($dirstring); # Unfortunately, splitpath's doc says: # The directory portion may or may not be returned with a trailing + '/'. # # Which 'may or may not' give us an extra 'null' member of # @dirs, because splitdir allows null directory names. # # Hence this next line, purely for cleanup: pop @dirs if ($dirs[-1] eq ''); $file =~ s/\.pm$//; my $package = join('::',@dirs,$file);

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
      Thanks. I was looking for something like splitdir, but for some reason I was looking in File::Basename ... :)
        I often was confused by that as well. I would mix up File::Basename, File::stat, and File::Spec, there should probably be a man/perldoc page that lists all of the modules that come packaged with perl, along with a one line desc of their task. Much like a info coreutils on debian (which I still find extremly usefull.)


        Evan Carroll
        www.EvanCarroll.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://500492]
Approved by sk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found