http://qs1969.pair.com?node_id=609697

brusimm has asked for the wisdom of the Perl Monks concerning the following question:

A question has been posed to me that I am not sure how to go about it and searches on perldoc.perl.org and here have not turned up what I am seeking. (Or I haven't put in the correct question.) And I am seeking wisdom Much greater than my own. (I'm making an assumption I have some form of wisdom)

I have a routine at \u\site1\program\routine.
This routine uses an answer file, and the default answer file is located also, in \u\site1\program\, with the routine.
When it is called, there are 2 ways to call it.
1- by invoking \u\site1\program\routine and the default answer file is used.
2- Invoking the routine, followed by another path if a user has a customized answer file,
IE: \u\site1\program\routine \u\site2\userfolder\answerfile

I understand system calls for the user such as getting the user name, or pwd. But I only understand it as a local function when the user is running a routine from the directory they are in. In this case, this is a company wide used routine, used by many. The desire is for users to have their own answer files in their own directories, and invoke the routine, but rather than the users actually typing out the path to their own answer file, ('cause ya know, copying and pasting can be tough some days) have the routine 'know' where the location the user is in when they invoke it. (Assumption or Rule: The user will be in the directory their answer file is located.)

Is there a function or methodology for the user to invoke the routine, but having the routine locate OR understand where the user is running the routine from or is this even doable?

Thanks all. Bruce

Replies are listed 'Best First'.
Re: Locating A Users Current Remote Directory
by ikegami (Patriarch) on Apr 12, 2007 at 17:00 UTC

    Given The user will be in the directory their answer file is located,

    my $file_name = @ARGV ? shift(@ARGV) : 'answerfile'; open(my $fh, '<', $file_name) or die("Unable to open answer file \"$file_name\": $!\n");

    If you wanted to know the full path,

    use File::Spec::Functions qw( rel2abs ); my $full_name = rel2abs($file_name);

    If you wanted to know the directory,

    use File::Basename qw( dirname ); my $dir_name = dirname($full_name);

    Update: Here it is in practice:

    E:\some\path>type ..\bin\routine.pl use strict; use warnings; use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); my $file_name = @ARGV ? shift(@ARGV) : 'answerfile'; open(my $fh, '<', $file_name) or die("Unable to open answer file \"$file_name\": $!\n"); my $full_name = rel2abs($file_name); my $dir_name = dirname($full_name); print("Name: $file_name\n"); print("Full Name: $full_name\n"); print("Dir Name: $dir_name\n"); print("Contents:\n"); print while <$fh>; E:\some\path>type answerfile Answer file in \some\path E:\some\path>type ..\otherpath\answerfile Answer file in \some\otherpath E:\some\path>..\bin\routine.pl Name: answerfile Full Name: E:\some\path\answerfile Dir Name: E:\some\path Contents: Answer file in \some\path E:\some\path>..\bin\routine.pl ..\otherpath\answerfile Name: ..\otherpath\answerfile Full Name: E:\some\path\..\otherpath\answerfile Dir Name: E:\some\path\..\otherpath Contents: Answer file in \some\otherpath E:\some\path>..\bin\routine.pl e:\some\otherpath\answerfile Name: e:\some\otherpath\answerfile Full Name: E:\some\otherpath\answerfile Dir Name: E:\some\otherpath Contents: Answer file in \some\otherpath

    I used windows, but it works without changes in unix.

Re: Locating A Users Current Remote Directory
by ferreira (Chaplain) on Apr 12, 2007 at 20:31 UTC
    Is there a function or methodology for the user to invoke the routine, but having the routine locate OR understand where the user is running the routine from or is this even doable?
    I am not 100% sure I fully understood your problem (mainly the thing about "current remote directory"), but if it is to determine the home directory of some user, this functionality is included in File::HomeDir. It may not be perfect all the time (as the documentation itself says), but it may be a good departing point.

    If it fits you, probably all you have to do is

    use File::HomeDir; $home = File::HomeDir->users_home('foo');
      ferreira: I'm not determining just the home directory, but the exact directory a user has navigated to, to be able to run the procedure noted and have it use the answer file in that directory. It may not even be that users own directory structure they run the procedure from.

      Here's another example: I am calling a routine:
      \u\user100\idaho\perl\functions\sort.pl But I am calling it from:
      \u\userabc\calif\files\answer\

      I want to enable an option in sort.pl to know the exact location I am in when I invoke it so it looks for an answer file at my location.

      (This whole question is brought about because some users don't want to have to type the path to their own answer files.)

      btw: Thanks Ikegami for your time.