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

This works
read_in_file(); sub read_in_file { open(INPUTFILE, "</WEBDEVELOPMENT/cgi-bin/postcode.txt"); }

This doesnt
use lib qw( "</WEBDEVELOPMENT/cgi-bin/ ); read_in_file(); sub read_in_file { open(INPUTFILE, "<postcode.txt"); }

Does anyone know why this could be? I dont want to hard code the path within the code. I just want it in a 'use lib'.

Replies are listed 'Best First'.
Re: lib
by arhuman (Vicar) on Apr 11, 2001 at 14:21 UTC
    lib is used by 'Perl logic' (related to require, use... and the @INC array )
    I doesn't deal with 'user logic' when you open data files or whatever you want to open.

    Rephrased in clear way :
    the lib module is only a way to manipulate easily @INC which is used by perl to load modules/code (via require or use)

    "Only Bad Coders Badly Code In Perl" (OBC2IP)
Re: lib
by physi (Friar) on Apr 11, 2001 at 14:28 UTC
    AFAIK use lib extends your @INC path.
    So you can include libraries (modules .pm) by
    use lib "path/to/your/modules"; use Module;
    It has nothing to do with the path to any files you want to open.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
      Okay

      One other thing, I want to open this file (which is in the current directory) but it wont let me unless i type in the full path from root.

      open(INPUTFILE, "<postcode.txt")

      I have to do this

      open(INPUTFILE, "</WEBDEV/cgi-bin/postcode.txt")

      Surely the perl knows to look from the existinf directory (eg ".")
      Re: lib
      by kal (Hermit) on Apr 11, 2001 at 15:47 UTC

        If you're running it as a CGI you don't necessarily know what the current directory is. Try doing a print `pwd`; to see where the script actually is.

        Usually, with things like file paths, it can be useful to make a configuration variable that holds this stuff in, so when you open you do something more like open (INPUTFILE, "<$root/postcode.txt");. $root can be a variable set at the top of the file, read in from a configuration file, etc.

Re: lib
by alfie (Pilgrim) on Apr 11, 2001 at 17:38 UTC
    Maybe the -P switch is what you want. You can use then the following construct:
    #!/usr/bin/perl -P #define MYPATH "/WEBDEVELOPMENT/cgi-bin/postcode.txt" read_in_file(); sub read_in_file { open(INPUTFILE, "<".MYPATH."postcode.txt"); }
    That way you can define things at the top of your file and use the defined values through your code. See perlman:perlrun for more information about that switch.
    --
    Alfie