Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

relative paths in perl...

Status
Not open for further replies.

plasmatwin

Programmer
Nov 1, 2005
31
GB
Hi,

In windows, the $0 variable returns the FULL path to my perl script, but in linux it is giving me a relative path - not useful to me at the moment. as soon as I chdir() to another directory (eg "/"), the relative path is no longer useful to me and $0 still contains the path relative to where I was at runtime. Any solutions to this problem?
 
I don't know of any simple cure, but you could write a little subroutine to return the full path. It could parse the path to see if it's absolute or relative, and if the latter, prefix it with the current directory. Obviously it would have to be run at the start of the program, before any chdiring.
 
Code:
sub finddir
{
	if (dirname($0) !~ /^./) { return dirname($0); }
	elsif (`pwd`)
	{
		my $dir = `pwd`;
		chomp($dir);
		my $dir2 = $0;
		$dir2 =~ s/^.//;
		$dir = $dir . $dir2;
		return dirname($dir);
	}
	else { return 0; }
}

perhaps? It's untidy so if someone can help me tidy it up I would be appreciative, and it will only work before any chdir()ing (I think). Any suggestions/improvements?
 
I tested the problem on Linux, and what seems to happen is that the path is given relative to the user's home directory; if the script lies outside the home directory the absolute path is given.

I was wrong about appending the current directory; that would give the directory the script was called from, not the directory of the script itself.

I suppose the solution would be that, if the path is deemed to be relative, prefix the user's home directory.

So, how do you recognise a relative path? A Windows path (which is apparently always absolute) always contains a backslash. In Linux, an absolute path starts with a slash, a relative path doesn't.

So, this code might work:
[tt]
sub scriptpath
{
if($0=~/\\/||$0=~/^\//){return $0}
return "$ENV{HOME}/$0";
}
[/tt]
 
mine was giving it relative to where I called it from... if you look at my solution, then you can see I have appended the relative path to the current directory (so the path to where I am, then the path from there to the script). Windows gives it with the C: (or whatever) at the beginning, so I detect relative paths by looking for a ./ at the beginning (which is what I was getting from relative paths, anyway). I have tested it and everything works apart from after I chdir() into another directory, however I have avoided this by making sure I keep the variable that stores the full path forever. Not the best solution I think, but the best I can come up with for now. Thanks for your help with this. (Any improvement on the code I posted above is welcomed :))
 
sorry, I detect them by looking for a "." at the beginning, there might not be a following slash.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top