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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.