in reply to Perl Script own path
In Perl, the automatic variable $0 in some operating systems gives the full path of the currently executing Perl script. In others you just get the name of the file that is executing. The module Cwd will give you the "current working directory". Just see attached code... executed on a Windows 32 bit XP system and a 64 bit Linux system. The combination of basename() and cwd() will always work on all OS'es.
Moral of the story:#!/usr/bin/perl -w use strict; use File::Basename; use Cwd; print "dollar 0 is $0\n"; print basename($0), "\n"; print "current working directory is: ",cwd(),"\n"; __END__ On Windows, you get: dollar 0 is C:\TEMP\exename.pl exename.pl current working directory is: C:/TEMP On Linux, you get: dollar 0 is ./exename.pl exename.pl current working directory is: /home/xxx/yy
Update: Note the Windows cwd() results in "C:/TEMP"! Windows allows the use of the forward-slash ('/') in almost all situations. There is a situation where that is not true (the backslash '\' is required), but I can't remember at the moment what that is. Some Monk will probably enlighten us. My main point is that '/' almost always works to join paths on Windows.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Script own path
by morgon (Priest) on Jul 25, 2011 at 05:05 UTC | |
by Marshall (Canon) on Jul 25, 2011 at 05:21 UTC | |
by afoken (Chancellor) on Jul 25, 2011 at 13:27 UTC | |
|
Re^2: Perl Script own path
by Jim (Curate) on Jul 27, 2011 at 05:30 UTC | |
by Marshall (Canon) on Jul 27, 2011 at 07:51 UTC | |
by Anonymous Monk on Jul 27, 2011 at 07:57 UTC | |
by Marshall (Canon) on Jul 27, 2011 at 08:20 UTC |