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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.