perl include 文件--------require

perl的包含文件
require ("myfile.pl");

        基本的包含文件全过程
  • 建立一个include文件,就按整个的perl script文件格式建立,文件中写sub
[macg@localhost perltest]$ vi file1.pl

#!/usr/bin/perl

sub gotest{
my(@tmp)=@_;

open (MYFILE, $tmp[0]) || die ("Could not open file");
my($line,$newline);
while ($line=<MYFILE>) {
$line=~ tr/a-zA-Z//s;
  print "good \n";
  print "\$line is :$line";
  print "\$\& is : $&", "\n";
  }

close(MYFILE);
   
  • 主perl文件require上边的包含文件,引用定义在包含文件中的sub
[macg@localhost perltest]$ vi tip.pl
#!/usr/bin/perl

require ("file1.pl");

$file="/home/macg/perltest/gogo";
&gotest($file);

@arrary=qw(a b cc dd);
$pointer = \@arrary;
for($i=0;$i<4;$i++) {
   print "$i : $pointer->[$i] \n";  }
[macg@localhost perltest]$ ./tip.pl
file1.pl did not return a true value at ./tip.pl line 3.
     

   上面哪里错了? 包含文件,必须return一个1
[macg@localhost perltest]$ vi file1.pl

#!/usr/bin/perl

sub gotest{
my(@tmp)=@_;

open (MYFILE, $tmp[0]) || die ("Could not open file");
my($line,$newline);
while ($line=<MYFILE>) {
$line=~ tr/a-zA-Z//s;
  print "good \n";
  print "\$line is :$line";
  print "\$\& is : $&", "\n";
  }

close(MYFILE);
}

1;   
[macg@localhost perltest]$ ./tip.pl
good
$line is :I WANTWANT TO go to NY. And I also wWant to be DC.


             require;           不指定文件名
变量 $_ 的值即作为文件名传递给require
 

        require   ("testdir/file1.pl");     允许带路径名
#!/usr/bin/perl

require ("testdir/file1.pl");

$file="/home/macg/perltest/gogo";
&gotest($file);
 
[macg@localhost perltest]$ ls -F
file1.pl  gogo  newdir/  newtest  test  testdir/  tip.pl*
[macg@localhost perltest]$ ls testdir
file1.pl  test1

[macg@localhost perltest]$ ./tip.pl
good
$line is :I WANTWANT TO go to NY. And I also wWant to be DC


        不用全路径名,而require文件又不在当前路径下,怎么办?  ----@INC
[macg@localhost perltest]$ vi tip.pl

#!/usr/bin/perl

require ("file1.pl");

$file="/home/macg/perltest/gogo";
&gotest($file);
[macg@localhost perltest]$ ./tip.pl
Can't locate file1.pl in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-linux-thread-multi at ./tip.pl line 3.
把file1.pl所在目录放入@INC系统变量中
[macg@localhost perltest]$ vi tip.pl
#!/usr/bin/perl

unshift (@INC, "/home/macg/perltest/testdir");
require ("file1.pl");

$file="/home/macg/perltest/gogo";
&gotest($file);
[macg@localhost perltest]$ ./tip.pl
good
$line is :I WANTWANT TO go to NY. And I also wWant to be DC


      @INC
[macg@localhost perltest]$ vi tip.pl

@arraytest=qq(hello "test" world);
#!/usr/bin/perl
foreach  (@INC) {
 print $_,"\n";
}
[macg@localhost perltest]$ ./tip.pl
/usr/lib/perl5/site_perl/5.8.6
/usr/lib/perl5/site_perl/5.8.5
/usr/lib/perl5/site_perl/5.8.4
/usr/lib/perl5/site_perl/5.8.3
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.6
/usr/lib/perl5/vendor_perl/5.8.5
/usr/lib/perl5/vendor_perl/5.8.4
/usr/lib/perl5/vendor_perl/5.8.3
/usr/lib/perl5/vendor_perl
/usr/lib/perl5/5.8.6/i386-linux-thread-multi
/usr/lib/perl5/5.8.6


        修改@INC
[macg@localhost perltest]$ vi tip.pl

#!/usr/bin/perl
$path1="/home/macg";
$path2="/home/macg/www";
push(@INC,$path1);      放尾部
unshift(@INC,$path2);  放头部
foreach  (@INC) {
 print $_,"\n";
[macg@localhost perltest]$ ./tip.pl
/home/macg/www
/usr/lib/perl5/vendor_perl/5.8.6
/usr/lib/perl5/vendor_perl/5.8.5
/usr/lib/perl5/vendor_perl/5.8.4
/usr/lib/perl5/vendor_perl/5.8.3
/usr/lib/perl5/vendor_perl
/usr/lib/perl5/5.8.6
.
/home/macg 
   
        push 或unshift @INC只对当前文件执行时刻有效,没有global效果
<think>好的,用户想了解cgi-bin目录的作用及配置方法。首先,我需要回忆一下CGI的基本概念。CGI是通用网关接口,允许Web服务器执行外部程序,通常用于生成动态内容。cgi-bin目录是存放这些CGI脚本的地方,比如Perl或Python脚本。 接下来,我得整理cgi-bin目录的主要作用。应该是用来集中管理CGI程序,确保服务器安全地执行这些脚本,避免任意目录执行带来的风险。然后,配置方法可能涉及Apache或Nginx的服务器设置,需要修改配置文件,指定cgi-bin的路径,设置权限,还有可能涉及模块的加载,比如mod_cgi或mod_cgid。 用户提供的引用中有几个关键点。引用1提到默认目录是/var/www/cgi-bin,引用2和3讨论了Apache的配置,比如加载模块和修改serve-cgi-bin.conf文件。引用3还给出了一个Perl脚本的例子,说明如何设置脚本的路径和权限。引用5提到了安装httpd并启动,这可能和Apache服务器的安装有关。 需要确保回答结构清晰,分点说明作用和配置步骤。配置部分可能需要分服务器类型,比如Apache和Nginx的不同方法。同时,要检查是否需要启用模块,修改目录权限,以及测试脚本的示例。可能还要提到常见问题,比如权限不足或路径错误,以及如何解决这些问题。 另外,用户可能对安全性有疑问,比如为什么需要特定目录,或者如何防止脚本被恶意执行。不过用户主要问的是作用和配置,可能不需要深入安全细节,但可以简要提及。 最后,生成相关问题时要围绕cgi-bin的用途、配置步骤、不同服务器的差异、脚本示例和常见问题处理。确保问题覆盖用户可能进一步想知道的内容。</think>### cgi-bin目录的作用及配置方法 #### 一、cgi-bin目录的作用 1. **CGI脚本存储**:用于存放通过通用网关接口(CGI)运行的脚本或程序(如Perl、Python、Shell等),实现动态网页功能[^1]。 2. **权限隔离**:限制CGI脚本的执行范围,防止任意目录下的文件被服务器执行,提升安全性[^3]。 3. **统一管理**:通过固定目录(如默认的`/var/www/cgi-bin`)集中管理可执行脚本,便于服务器配置和维护[^1]。 --- #### 二、配置方法(以Apache为例) ##### 1. **基础配置** - **启用CGI模块**: ```apache LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so # 加载模块[^2] ``` - **设置目录权限**: ```apache <Directory "F:/project/perl/cgi-bin/"> Options +ExecCGI AllowOverride None Require all granted AddHandler cgi-script .pl .cgi # 指定可执行脚本后缀 </Directory> ``` ##### 2. **路径配置** - 修改`serve-cgi-bin.conf`文件,将`ScriptAlias`指向实际目录: ```apache ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" # 默认路径[^1] ``` 或自定义路径(如Windows): ```apache ScriptAlias /cgi-bin/ "F:/project/perl/cgi-bin/" # 自定义路径[^3] ``` ##### 3. **脚本权限设置** - 脚本需添加执行权限(Linux): ```bash chmod +x /var/www/cgi-bin/hello.pl ``` - 脚本首行指定解释器路径(例如Perl): ```perl #!E:/webserver/perl/bin/perl.exe # Windows示例[^3] ``` ##### 4. **重启服务** ```bash systemctl restart apache2 # Linux # 或通过服务管理器重启Apache(Windows) ``` --- #### 三、验证配置 创建测试脚本`hello.pl`: ```perl #!/usr/bin/perl print "Content-type: text/html\n\n"; print "Hello, CGI!"; ``` 访问`http://localhost/cgi-bin/hello.pl`,若输出`Hello, CGI!`则配置成功。 --- #### 四、其他服务器配置(Nginx) 需通过反向代理或FastCGI实现,例如结合uWSGI: ```nginx location /cgi-bin/ { include uwsgi_params; uwsgi_pass unix:/tmp/uwsgi.sock; # 多文件启动参考[^4] } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值