Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

$Bin undef with FindBin in web environment

by Anonymous Monk
on Aug 30, 2004 at 07:48 UTC ( [id://386861]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Recently I asked for help with a FindBin problem. Davorg's solution fixed the problem of FindBin not working with taint. However, I have encountered another problem with FindBin.

When I use it within a web environment, the value of $Bin is undef. This is a problem, because the reason I wanted to use FindBin in the first place was to get around the problem of different relative paths for command-line versus web use, without hardcoding an absolute path in to every script.

The basic structure of the project is a root directory, which could change depending upon the server the project is running on. Inside the root directory are several directories, including lib and cgi-bin. cgi-bin is a web root directory. From the command line, lib can be reached with ../lib, but from a web environment this obviously doesn't work. What I hoped to do was to resolve the real (not web) absolute path for the script and determine the absolute path to the lib directory from that.

Can someone suggest another solution to this problem?

I have not included any code because it can be viewed in the previous (linked) node.

Thank you for your help.

  • Comment on $Bin undef with FindBin in web environment

Replies are listed 'Best First'.
Re: $Bin undef with FindBin in web environment
by tachyon (Chancellor) on Aug 30, 2004 at 08:15 UTC

    Can someone suggest another solution to this problem?

    1. Use a cgi-bin/YourLib/Widget.pm stucture ie use lib './YourLib' is all you need in your CGIs
      • this breaks if you call the script without cgi-bin being the cwd as pointed out by Corion
    2. Install your libs in the usual Perl way as this removes the lib issue as @INC covers it.
    3. Using configuration constants, set up on install gives yet another option.

    cheers

    tachyon

      Thank you for your suggestions.

      Regarding 1, isn't it generally a bad idea to place lib directories within a web root directory? In addition to mixing up different parts of the project, it makes it easier to mistakenly serve modules.

      Regarding 2, it is my preference to place all files associated with a project within a single 'project' directory. This makes it easier to find all files associated with the project, or to check out the project in one step.

      Three is a realistic option for a packaged product, but I'm not sure it is appropriate for a work in progress which will in the end only be installed on two servers. It seems like quite a lot of overhead (in time) to make a formal install process.

        The assumption with suggestion 1 is that your cgi-bin directory is outside the document root, as in the typical $somepath/htdocs/ + $somepath/cgi-bin/ scenario. If your cgi-bin is inside the document root as in $somepath/htdocs/cgi-bin/, then you would indeed be at risk of serving modules as plaintext — but you risk the same for your scripts, as well.

        Makeshifts last the longest.

Re: $Bin undef with FindBin in web environment
by Aristotle (Chancellor) on Aug 30, 2004 at 11:29 UTC

    Note that FindBin is broken. As that node mentions, you want something like

    use File::Spec::Functions qw( rel2abs ); print "I am ", rel2abs( $0 ), "\n";

    Makeshifts last the longest.

      Thank you. This almost does what I want. Unfortunately, the result contains a symbolic link and I would like to know where the file actually is. I read the perldoc for File::Spec but did not find anything that would solve my problem.

      Would you happen to know of a function which does that?

        Try abs_path from Cwd.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found