On Thu, May 09, 2002 at 11:22:07AM -0400, Shishir K. Singh wrote:
> a)Is there any function that returns the file path format depending on the
>   OS name?

Not that I know of.  There is, however, a couple of modules for dealing with
paths.

First, there's File::Basename for splitting off the directory, file, and
extension portions of a path.  It's pretty easy to use.

Next, there's File::Spec.  It's a whole suite of class methods for splitting
up paths into whatever units your heart desires; drive letter, directory,
filename, etc.  Of course, with this added capability comes added
complexity.


> b) How can I write this split by using a variable @elements = split
>    (/\\/,$string); I want to pass a variable instead of hard coding "\\"
>    in the split.

If you want to be portable to more than just Windows- and Unix-based
filesystems you can't.  On some OSs splitting paths is not a simple matter
of finding the seperator character.

Also, if you were to use your split on Unix (splitting on /) you'd get
incorrect results.  The path /etc/passwd would be split up into ("", "etc",
"passwd").

To split your path as portably as possible:

    use File::Spec;

    my($volume, $dir, $file) = File::Spec->splitpath($string);
    my @dir = File::Spec->splitdir($dir);

>From that, $volume contains your volume name (such as C: on Windows), @dir
your directory elements, and $file the filename.

See perldoc File::Spec for more information.  For education on how other
operating systems deal with paths, see perldoc File::Spec::Mac and perldoc
File::Spec::VMS.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to