Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: How to determine absolute path of current Perl file?

by hakonhagland (Scribe)
on Mar 01, 2016 at 09:58 UTC ( [id://1156509]=note: print w/replies, xml ) Need Help??


in reply to Re: How to determine absolute path of current Perl file?
in thread How to determine absolute path of current Perl file?

Hi Tanktalus!

Avoiding PadWalker: mind saying why?

The reluctance to use PadWalker was merely based on information I could read about it in the source and Pod documentation of Data::Dumper::Simple. It says:

Note that if you strongly object to source filters, I've also released Data::Dumper::Names. It does what this module does by it uses PadWalker instead of a source filter. Unfortunately, it has a few limitations and is not as powerful as this module. Think of Data::Dumper::Names as a "proof of concept".
Also Data::Dumper::Names says:
This module is an alternative to Data::Dumper::Simple. Many people like the aforementioned module but do not like the fact that it uses a source filter. In order to pull off the trick with this module, we use PadWalker. This introduces its own set of problems, not the least of which is that PadWalker uses undocumented features of the Perl internals and has an annoying tendency to break. Thus, if this module doesn't work on your machine you may have to go back to Data::Dumper::Simple.

Now, I tested PadWalker quickly today to check how it works for some simple cases. And based on these tests I have a gut feeling that it may not be as general solution as using PPI. Firstly, I noticed that it can only access lexical variable names in the argument list. This is currently a severe limitation since very often you want to print out package variables. On the other hand, a good thing about PadWalker seems to be that it can easily differentiate between calls like p $var and p func($var). (The p function here is the call to Data::Printer::p()). So it will understand that in the latter call $var is not the sought printed variable. This is more difficult with PPI, but it should be possible.

Having said that, if you want the full path name to the current file, just use __FILE__. And if you want the full path name to the file that just called you, just use (caller)1. As a bonus, caller will also give you the line number that you were called from.

Yes exactly, that linenumber information given by caller() was what I was trying to exploit! However, the problem of determining the absolute path of the filename still persists, since __FILE__ can be a relative pathname. I am curious why the Perl porters did not make __FILE__ always be an absolute pathname. I cannot see any benefits of populating it with a relative pathname :)

Replies are listed 'Best First'.
Re^3: How to determine absolute path of current Perl file?
by LanX (Saint) on Mar 01, 2016 at 13:29 UTC
    > Note that if you strongly object to source filters, I've also released Data::Dumper::Names. It does what this module does by it uses PadWalker instead of a source filter. Unfortunately, it has a few limitations and is not as powerful as this module. Think of Data::Dumper::Names as a "proof of concept".

    Ovid didn't update this text for years, but last time I talked to him he was very confident about this being stable.

    But please note that PadWalker has still limitations, like when being called from within an eval .

    see PadWalker::var_name BUG?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re^3: How to determine absolute path of current Perl file?
by Your Mother (Archbishop) on Mar 01, 2016 at 13:01 UTC

    Regarding benefits of non-absolute __FILE__. I suspect it is because the information requires consulting the file system, perhaps at length, to resolve links and such. Often unnecessary and always more expensive.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1156509]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found