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.
In reply to Re: Locating A Users Current Remote Directory
by ikegami
in thread Locating A Users Current Remote Directory
by brusimm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |