tags:

views:

77

answers:

2

I want to use files that are inside cgi-bin folder in perl, I want to call them from index.cgi When I do this: use cgi-bin/file_name it doesn't work, how can I do that?

A: 

You could try "do"

# read in config files: system first, then user
for $file ("/share/prog/defaults.rc", "$ENV{HOME}/.someprogrc")
{
   unless ($return = do $file) {
     warn "couldn't parse $file: $@" if $@;
     warn "couldn't do $file: $!"    unless defined $return;
     warn "couldn't run $file"       unless $return;
   }
}

This is from perldoc example.

krico
thanks it worked.
zeina
+6  A: 

When you ask Perl to use a module, it searches the directories in @INC. If a module you want to use is located in some other directory, you can do this:

use lib 'some/other/directory';
use TheModule;

If you don't want the directory to be added to @INC, you can implement use directly like this:

BEGIN {
    require 'some/other/directory/TheModule.pm';
    TheModule->import('foo', 'bar', ...);
}

More details on use, use lib and @INC.

FM
I assuemd these were no modules, but scripts since they are under cgi-bin/I finde it a little dangerous to add cgi-bin/ to @INC, don't you?
krico
@krico If they are not modules, why are they `pm` files? Regarding the concern about adding `cgi-bin` to `@INC`, please see the edited answer.
FM
makes sense. I like it.
krico