📄 homedir.pm
字号:
package File::HomeDir;# See POD at end for docsuse 5.005;use strict;use Carp ();use File::Spec ();# Globalsuse vars qw{$VERSION @ISA @EXPORT @EXPORT_OK $IMPLEMENTED_BY};BEGIN { $VERSION = '0.67'; # Inherit manually require Exporter; @ISA = qw{ Exporter }; @EXPORT = qw{ home }; @EXPORT_OK = qw{ home my_home my_desktop my_documents my_music my_pictures my_videos my_data users_home users_desktop users_documents users_music users_pictures users_videos users_data }; # %~ doesn't need (and won't take) exporting, as it's a magic # symbol name that's always looked for in package 'main'.}# Don't do platform detection at compile-timeif ( $^O eq 'MSWin32' ) { # All versions of Windows $IMPLEMENTED_BY = 'File::HomeDir::Windows'; require File::HomeDir::Windows;} elsif ( $^O eq 'darwin' ) { # Modern Max OS X $IMPLEMENTED_BY = 'File::HomeDir::Darwin'; require File::HomeDir::Darwin;} elsif ( $^O eq 'MacOS' ) { # Legacy Mac OS $IMPLEMENTED_BY = 'File::HomeDir::MacOS9'; require File::HomeDir::MacOS9;} else { # Default to Unix semantics $IMPLEMENTED_BY = 'File::HomeDir::Unix'; require File::HomeDir::Unix;}###################################################################### Current User Methodssub my_home { $IMPLEMENTED_BY->my_home;}sub my_desktop { $IMPLEMENTED_BY->can('my_desktop') ? $IMPLEMENTED_BY->my_desktop : Carp::croak("The my_desktop method is not implemented on this platform");}sub my_documents { $IMPLEMENTED_BY->can('my_documents') ? $IMPLEMENTED_BY->my_documents : Carp::croak("The my_documents method is not implemented on this platform");}sub my_music { $IMPLEMENTED_BY->can('my_music') ? $IMPLEMENTED_BY->my_music : Carp::croak("The my_music method is not implemented on this platform");}sub my_pictures { $IMPLEMENTED_BY->can('my_pictures') ? $IMPLEMENTED_BY->my_pictures : Carp::croak("The my_pictures method is not implemented on this platform");}sub my_videos { $IMPLEMENTED_BY->can('my_videos') ? $IMPLEMENTED_BY->my_videos : Carp::croak("The my_videos method is not implemented on this platform");}sub my_data { $IMPLEMENTED_BY->can('my_data') ? $IMPLEMENTED_BY->my_data : Carp::croak("The my_data method is not implemented on this platform");}###################################################################### General User Methodssub users_home { $IMPLEMENTED_BY->can('users_home') ? $IMPLEMENTED_BY->users_home( $_[-1] ) : Carp::croak("The users_home method is not implemented on this platform");}sub users_desktop { $IMPLEMENTED_BY->can('users_desktop') ? $IMPLEMENTED_BY->users_desktop( $_[-1] ) : Carp::croak("The users_desktop method is not implemented on this platform");}sub users_documents { $IMPLEMENTED_BY->can('users_documents') ? $IMPLEMENTED_BY->users_documents( $_[-1] ) : Carp::croak("The users_documents method is not implemented on this platform");}sub users_music { $IMPLEMENTED_BY->can('users_music') ? $IMPLEMENTED_BY->users_music( $_[-1] ) : Carp::croak("The users_music method is not implemented on this platform");}sub users_pictures { $IMPLEMENTED_BY->can('users_pictures') ? $IMPLEMENTED_BY->users_pictures( $_[-1] ) : Carp::croak("The users_pictures method is not implemented on this platform");}sub users_videos { $IMPLEMENTED_BY->can('users_videos') ? $IMPLEMENTED_BY->users_videos( $_[-1] ) : Carp::croak("The users_videos method is not implemented on this platform");}sub users_data { $IMPLEMENTED_BY->can('users_data') ? $IMPLEMENTED_BY->users_data( $_[-1] ) : Carp::croak("The users_data method is not implemented on this platform");}###################################################################### Legacy Methods# Find the home directory of an arbitrary usersub home (;$) { # Allow to be called as a method if ( $_[0] and $_[0] eq 'File::HomeDir' ) { shift(); } # No params means my home return my_home() unless @_; # Check the param my $name = shift; if ( ! defined $name ) { Carp::croak("Can't use undef as a username"); } if ( ! length $name ) { Carp::croak("Can't use empty-string (\"\") as a username"); } # A dot also means my home ### Is this meant to mean File::Spec->curdir? if ( $name eq '.' ) { return my_home(); } # Now hand off to the implementor $IMPLEMENTED_BY->users_home($name);}###################################################################### Tie-Based Interface# Okay, things below this point get scaryCLASS: { # Make the class for the %~ tied hash: package File::HomeDir::TIE; # Make the singleton object. # (We don't use the hash for anything, though) ### THEN WHY MAKE IT??? my $SINGLETON = bless {}; sub TIEHASH { $SINGLETON } sub FETCH { # Catch a bad username unless ( defined $_[1] ) { Carp::croak("Can't use undef as a username"); } # Get our homedir unless ( length $_[1] ) { return File::HomeDir::my_home(); } # Get a named user's homedir return File::HomeDir::home($_[1]); } sub STORE { _bad('STORE') } sub EXISTS { _bad('EXISTS') } sub DELETE { _bad('DELETE') } sub CLEAR { _bad('CLEAR') } sub FIRSTKEY { _bad('FIRSTKEY') } sub NEXTKEY { _bad('NEXTKEY') } sub _bad ($) { Carp::croak("You can't $_[0] with the %~ hash") }}# Do the actual tie of the global %~ variabletie %~, 'File::HomeDir::TIE';1;__END__=pod=head1 NAMEFile::HomeDir - Find your home and other directories, on any platform=head1 SYNOPSIS use File::HomeDir; # Modern Interface (Current User) $home = File::HomeDir->my_home; $desktop = File::HomeDir->my_desktop; $docs = File::HomeDir->my_documents; $music = File::HomeDir->my_music; $pics = File::HomeDir->my_pictures; $videos = File::HomeDir->my_videos; $data = File::HomeDir->my_data; # Modern Interface (Other Users) $home = File::HomeDir->users_home('foo'); $desktop = File::HomeDir->users_desktop('foo'); $docs = File::HomeDir->users_documents('foo'); $music = File::HomeDir->users_music('foo'); $pics = File::HomeDir->users_pictures('foo'); $video = File::HomeDir->users_videos('foo'); $data = File::HomeDir->users_data('foo'); # Legacy Interfaces print "My dir is ", home(), " and root's is ", home('root'), "\n"; print "My dir is $~{''} and root's is $~{root}\n"; # These both print the same thing, something like: # "My dir is /home/user/mojo and root's is /"=head1 DESCRIPTIONB<File::HomeDir> is a module for dealing with issues relating to thelocation of directories that are "owned" by a user, primarily your user,and to solve these issues consistently across a wide variety ofplatforms.Thus, a single API is presented that can find your resources on anyplatform.This module provides two main interfaces.The first is a modern L<File::Spec>-style interface with a consistentOO API and different implementation modules to support variousplatforms. You are B<strongly> recommended to use this interface.The second interface is for legacy support of the original 0.07 interfacethat exported a C<home()> function by default and tied the C<%~> variable.It is generally not recommended that you use this interface, but due toback-compatibility reasons they will remain supported until at least 2010.After this date, the home() function will remain, but we will considerdeprecating the (namespace-polluting) C<%~> tied hash, to be removed by
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -