Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Windows - dirname(__FILE__)

by demichi (Acolyte)
on Apr 11, 2014 at 11:33 UTC ( #1081949=perlquestion: print w/ replies, xml ) Need Help??
demichi has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have the following code.

use strict; use warnings; use File::Basename; my $script_dirname = dirname(__FILE__); print "dirname: $script_dirname\n";

When I run the script I get the following output:

C:\Data\scripting\misc>path_test.pl dirname: C:\Data\scripting\misc C:\Data\scripting\misc>

This is what I expect. I get the full path of the directory. All is good.

However when I run the script with the interpreter I get the following:

C:\Data\scripting\misc>perl path_test.pl dirname: . C:\Data\scripting\misc>

I just get the "." for the current directory.

Why is this the case? How can I always get easily the full path of the directoy where my script is running - no matter how the user calls the script?

(It must work on Windows and would be nice if this also runs on Unix).

Thanks for your help.

regards deMichi

Comment on Windows - dirname(__FILE__)
Select or Download Code
Re: Windows - dirname(__FILE__)
by demichi (Acolyte) on Apr 11, 2014 at 11:51 UTC
    Might that be an idea?
    use strict; use warnings; use Cwd 'abs_path'; use File::Basename; my $script_dirname = dirname(abs_path($0)); print "dirname: $script_dirname\n";

    Output

    C:\Data\scripting\misc>path_test.pl dirname: C:/Data/scripting/misc C:\Data\scripting\misc>perl path_test.pl dirname: C:\Data\scripting\misc C:\Data\scripting\misc>

    Just a bit strange for me that it change / to \ when using the interpreter ...

Re: Windows - dirname(__FILE__)
by choroba (Abbot) on Apr 11, 2014 at 12:31 UTC
    Are you looking for FindBin?
    use FindBin; print "Script running from ", $FindBin::Bin, "\n";
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Windows - dirname(__FILE__)
by fishy (Beadle) on Apr 11, 2014 at 16:48 UTC
    While your script don't chdir()
    #!/usr/bin/perl use strict; use warnings; use Cwd; my $script_dirname = cwd; print "dirname: $script_dirname\n";

      Sadly, this won't work when script is called from another directory by full path:

      $ pwd /home/aitap $ cat /tmp/1.pl #!/usr/bin/perl use strict; use warnings; use Cwd; my $script_dirname = cwd; print "dirname: $script_dirname\n"; use Cwd 'abs_path'; use File::Basename; $script_dirname = dirname(abs_path($0)); print "script dirname: $script_dirname\n"; $ perl /tmp/1.pl dirname: /home/aitap script dirname: /tmp

Re: Windows - dirname(__FILE__) ( Path::Tiny::path(__FILE__)->realpath->parent
by Anonymous Monk on Apr 11, 2014 at 21:46 UTC
    Path::Tiny
    use Path::Tiny qw/ path /; my $thisfile = path( __FILE__ )->realpath; my $thisdir = path( $thisfile )->parent;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081949]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2014-09-21 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls