Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: FindBin is broken (RE: How do I get the full path to the script executing?)

by merlyn (Sage)
on Jan 17, 2001 at 02:57 UTC ( [id://52398]=note: print w/replies, xml ) Need Help??


in reply to FindBin is broken (RE: How do I get the full path to the script executing?)
in thread How do I get the full path to the script executing?

Ahh. Suppose you call chdir before you call FindBin. I believe it will still work. But your rel2abs($0) will no longer be accurate.

So, with the caveat that you cannot call chdir before rel2abs, you can use it in place of FindBin.

-- Randal L. Schwartz, Perl hacker

  • Comment on Re: FindBin is broken (RE: How do I get the full path to the script executing?)

Replies are listed 'Best First'.
(tye)Re: FindBin is broken (RE: How do I get the full path to the script executing?)
by tye (Sage) on Feb 10, 2001 at 00:09 UTC

    I tried to come up with a situation like you described where FindBin.pm would work but rel2abs($0) wouldn't.

    The only case I found was pretty strange and, IMHO, just shows more ways in which FindBin.pm is broken. If you happen to be chdir()ed into a directory that is in your $ENV{PATH} and do "perl subdir/script" and subdir/script does a BEGIN { chdir "/tmp" } before you can do your rel2abs($0), then rel2abs($0) will be wrong (returning "/tmp/subdir/script").

    But FindBin.pm will see the "/" and assume that $ENV{PATH} doesn't need to be searched (which makes sense), but then will find that "./subdir/script" doesn't exist and so will change its mind. Then it will search $ENV{PATH} and will stop at the first path that actually contains a "subdir/script" in it. If that place also happens to be the directory that you happened to be in before the super-early chdir(), then FindBin.pm will have found, by accident, the correct path to the script.

    Personally I'd rather have FindBin.pm never search $ENV{PATH} and have it die "You chdir()ed too early" if ! -e $0 so that such strangeness could be detected rather than returning whatever it finds which might be right or might be wrong. That test isn't completely foolproof, but the ways to fool it are even more bizarre.

            - tye (but my friends call me "Tye")
      And that is why I wrote scriptname. :-)
      []s, HTH, Massa (κς,πμ,πλ)

        Then the next version of your module should have a name that starts with a capital letter and contains "::" in it (unless your module is actually a pragma and part of core, neither of which appear to be the case from here).

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found