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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relative path issue 1

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
I have a bunch of perl codes in a hierarchy somewhat like this:
Code:
[b]% ls *[/b]
demo1.pl*

subdir1:
demo2.pl*  mylib.pm

subdir2:
data.txt
Where data.txt is an ascii file and the contents of demo1.pl, demo2.pl & mylib.pm are list below:
Code:
[b]% cat demo1.pl[/b]
#! /usr/bin/perl

use lib "./subdir1";
use mylib;

print "This is $0...\n";
# More implementations here

[b]% cat ./subdir1/demo2.pl[/b]
#! /usr/bin/perl

use mylib;

print "This is $0...\n";
# More implementations here

[b]% cat ./subdir1/mylib.pm[/b]
package mylib;

[COLOR=blue]my $datafile = './subdir2/data.txt';   # To satisfy demo1.pl[/color]
[COLOR=red][b]#my $datafile = '../subdir2/data.txt'; # To satisfy demo2.pl[/b][/color]
if(-e $datafile) {
  print "File '$datafile' is found.\n";
}
else {
  print "File '$datafile' is NOT found.\n";
}

# some implementation here

1;
This package is intended to be installed in anywhere on any hosts in house. Therefore, there is no way to know the absolute path of ‘data.txt’.

However, both demo1.pl and demo2.pl are expected to be run from the current directory, i.e. ‘.’. So my problem is how to correctly define the relative path for ‘data.txt’ in mylib.pm? As the current implementation shown in mylib.pm, demo1.pl is run ok.
Code:
[b]% ./demo1.pl[/b]
File './subdir2/data.txt' is found.
This is ./demo1.pl...
But demo2.pl can not find ‘data.txt’, unless I commented out the blue line and uncommented the red line:
Code:
[b]% ./demo2.pl[/b]
[b]File './subdir2/data.txt' is NOT found.[/b]
This is ./demo2.pl...
I guess that I must have missed something that’s very basic.

Please help. Many thanks.
 
Then demo1.pl would not work.
 
The only way I can see to solve that without "changing the rules" is to check for the data file in both locations in mylib.pm.

Surely it would be better to run demo2.pl from the correct starting directory, i.e. using ./subdir1/demo2.pl.

If you can give some more details about why it needs to work this way we may be able to offer better advice...

Annihilannic.
 
The file hierarchy is inherited. However, before today, mylib.pm did not need to use data.txt. So both demo1.pl and demo2.pl were happy.

Now, we're thinking to let mylib.pm use data.txt. Since the tool are widely use in our organization and people are so used to run demo2.pl from the './', it would be hard for me to ask them run it from the upper level. It would be nice not let them even notice there was such a change.

So, please help.
 
I see. So how about my suggestion to check both? Something like this?

Code:
[url=http://perldoc.perl.org/functions/package.html][black][b]package[/b][/black][/url] [green]mylib[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$datafile_loc1[/blue] = [red]'[/red][purple]./subdir2/data.txt[/purple][red]'[/red][red];[/red]   [gray][i]# To satisfy demo1.pl[/i][/gray]
[black][b]my[/b][/black] [blue]$datafile_loc2[/blue] = [red]'[/red][purple]../subdir2/data.txt[/purple][red]'[/red][red];[/red] [gray][i]# To satisfy demo2.pl[/i][/gray]
[black][b]my[/b][/black] [blue]$datafile[/blue][red];[/red]
[olive][b]if[/b][/olive][red]([/red][url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [blue]$datafile_loc1[/blue][red])[/red] [red]{[/red]
  [blue]$datafile[/blue]=[blue]$datafile_loc1[/blue][red];[/red]
[red]}[/red] [olive][b]else[/b][/olive] [olive][b]if[/b][/olive] [red]([/red][black][b]-e[/b][/black] [blue]$datafile_loc2[/blue][red])[/red] [red]{[/red]
  [blue]$datafile[/blue]=[blue]$datafile_loc2[/blue][red];[/red]
[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
  [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]File '[blue]$datafile[/blue]' is NOT found.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]File '[blue]$datafile[/blue]' is found.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# some implementation here[/i][/gray]

Annihilannic.
 
Excellent!! Thank you, Annihilannic!!

Now I know how important the architecture is!!
 
Actually, you'll need to correct that die error message because the $datafile variable will be undefined...

Annihilannic.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top