maletin has asked for the wisdom of the Perl Monks concerning the following question:
How can I find out the absolute path of my source-code? I wrote a test-script that uses the function caller():
#! perl -w # filetest.t # should be invoked from different directories (prove -r). use strict; use warnings; use Test::More tests => 2; use File::Basename; use File::Spec; my $filename = (eval{caller})[1]; diag("filename of this test-script: $filename"); my $dirname = dirname( $filename ); diag("dirname of this test-script: $dirname"); my $location = File::Spec->catfile( $dirname, 'non-existing-file' ); diag("testing a non existing file: $location"); cmp_ok( defined -f $location, '==', 1, 'this test should fail' ); $location = File::Spec->catfile( $dirname, 'filetest.t' ); diag("testing an existing file: $location"); cmp_ok( defined -f $location, '==', 1, 'this file should exist' );
my output of perl lib/t/filetest.t:
How can I get the absolute filename instead of "./lib/t/filetest.t"?1..2 # filename of this test-script: lib/t/filetest.t # dirname of this test-script: lib/t # testing a non existing file: lib/t/non-existing-file not ok 1 - this test should fail # Failed test 'this test should fail' # at lib/t/filetest.t line 19. # got: # expected: 1 # testing an existing file: lib/t/filetest.t ok 2 - this file should exist # Looks like you failed 1 test of 2.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: absolute pathname
by ikegami (Patriarch) on Aug 12, 2008 at 20:57 UTC | |
by maletin (Sexton) on Aug 12, 2008 at 21:27 UTC | |
by ikegami (Patriarch) on Aug 12, 2008 at 21:44 UTC | |
Re: absolute pathname
by Lawliet (Curate) on Aug 12, 2008 at 22:57 UTC | |
by maletin (Sexton) on Aug 12, 2008 at 23:29 UTC | |
by ikegami (Patriarch) on Aug 12, 2008 at 23:24 UTC | |
Re: absolute pathname
by eyepopslikeamosquito (Archbishop) on Aug 12, 2008 at 21:28 UTC | |
by ikegami (Patriarch) on Aug 12, 2008 at 21:39 UTC |