Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-29 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found