Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

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

Background:

I am trying to improve the CPAN module Data::Printer such that it can display the variable name automatically (similarly to Data::Dumper::Simple). However, I will try to do this without using PadWalker or a source filter ( perlfilter or Filter::Util::Call ). Instead, I will try read the source file by exploiting filename and line number of current source from caller. Then parsing the line with PPI to recover the variable name.

Problem

My current problem is to determine the absolute path of the current Perl file. The problem occurs when the filename (as given by (caller)[1] ) is relative. For example running perl ./test/test.pl with ./test/test.pl:

use My::Module; My::Module::print_file_name();
and ./lib/My/Module.pm:
package My::Module; use feature qw(say); sub print_file_name { say __FILE__; } 1;
will give output lib/My/Module.pm (the same will perl function caller) which is a relative path name. My first attempt was to use FindBin (since I cannot rely on the current directory being the same as when the script was run):
if ( !File::Spec->file_name_is_absolute( $filename ) ) { $filename = File::Spec->rel2abs( $filename, $FindBin::Bin ); }
but the problem is that $filename is not relative to $FindBin::Bin so it will not work.

Then I considered using Module::Path, but this seemed like overkill for my use case. What I think I need is a simple module, say Cwd::Initial which gives me the initial working directory. For example:

package Cwd::Initial; use strict; use warnings; use Cwd (); our $cwd; our $VERSION = '0.10'; sub _getcwd { $cwd = Cwd::getcwd(); } BEGIN { _getcwd(); } sub getcwd { return $cwd; } 1;

Questions

Some questions:
  • Have I missed something? Is this functionality already available at CPAN?
  • If not, is the module Cwd::Initial really necessary? Should I instead suggest a modification to FindBin to expose the initial directory as part of its API?
  • Or, is the use case so special that I should rather not rely on a CPAN module to provide this but instead use a BEGIN{} block and a package variable our $inital_cwd?

In reply to How to determine absolute path of current Perl file? by hakonhagland

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 scrutinizing the Monastery: (3)
As of 2024-04-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found