在简单 Perl 教程中添加模块的包含路径
我正在尝试做一个简单的教程,但开始时遇到困难。我的问题似乎是安装和获取模块的正确路径。
**1.这是原始代码: *****
#!/usr/bin/perl -w
use strict;
use LWP 5.64;
my $browser = LWP::UserAgent->new;
my $url = 'http://www.cspan.org/RECENT.html';
my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
my $html = $response->content;
while( $html =~m/<A HREF=\"(.*?)\"/g ) {
print "$1\n";
2。但在 Host Gator 中,他们这样说:
你的 Perl 模块的位置
路径:/home/d********n/perl
使用你的 Perl 模块
你需要添加 / home/d********n/perl 到包含路径。您可以通过将以下代码添加到脚本中来完成此操作:
BEGIN {
my $base_module_dir = (-d '/home/d********n/perl' ? '/home/d********n/perl' : ( getpwuid($>) )[7] . '/perl/');
unshift @INC, map { $base_module_dir . $_ } @INC;
}
3.因此,我添加了代码,但不知道是否将其添加到正确的位置。
#!/usr/bin/perl -w
use strict;
use LWP 5.64;
BEGIN {
my $base_module_dir = (-d '/home/d********n/perl' ?
'/home/d********n/perl' : ( getpwuid($>) )[7] . '/perl/');
unshift @INC, map { $base_module_dir . $_ } @INC;
}
my $browser = LWP::UserAgent->new;
my $url = 'http://www.cspan.org/RECENT.html';
my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
my $html = $response->content;
while( $html =~m/<A HREF=\"(.*?)\"/g ) {
print "$1\n";
任何帮助将不胜感激。
仅供参考,我已经确保该文件具有所需的权限 755
另外,LWP::UserAgent 在 Host Gator 中的权限为 5.835。这是否意味着我必须更改
使用 LWP 5.64;
使用
LWP 5.835
I'm trying to do a simple tutorial but I'm having trouble getting started. My problem seems to be installing and getting the correct path to the modules.
**1. Here is the original code:*****
#!/usr/bin/perl -w
use strict;
use LWP 5.64;
my $browser = LWP::UserAgent->new;
my $url = 'http://www.cspan.org/RECENT.html';
my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
my $html = $response->content;
while( $html =~m/<A HREF=\"(.*?)\"/g ) {
print "$1\n";
2. But in Host Gator they say this:
Location of Your Perl Module(s)
Path: /home/d********n/perl
Using Your Perl Module(s)
You will need to add /home/d********n/perl to the include path. You can do this by adding the following code to your script:
BEGIN {
my $base_module_dir = (-d '/home/d********n/perl' ? '/home/d********n/perl' : ( getpwuid(gt;) )[7] . '/perl/');
unshift @INC, map { $base_module_dir . $_ } @INC;
}
3. So I added the code but have no idea if I added it in the correct spot.
#!/usr/bin/perl -w
use strict;
use LWP 5.64;
BEGIN {
my $base_module_dir = (-d '/home/d********n/perl' ?
'/home/d********n/perl' : ( getpwuid(gt;) )[7] . '/perl/');
unshift @INC, map { $base_module_dir . $_ } @INC;
}
my $browser = LWP::UserAgent->new;
my $url = 'http://www.cspan.org/RECENT.html';
my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
my $html = $response->content;
while( $html =~m/<A HREF=\"(.*?)\"/g ) {
print "$1\n";
Any help would be greatly appreciated.
FYI, I already made sure the file has the needed permissions 755
Also the LWP::UserAgent has a number of 5.835 in Host Gator. Does that mean I have to change
use LWP 5.64;
to
use LWP 5.835
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已将 LWP 安装在本地模块目录中,请将
BEGIN
块放在您尝试加载 LWP 之前(紧接在use strict
之后) 。原始代码中的版本号表明它是所需的最低版本。由于您已经有了较新的版本并且 LWP 的接口很稳定,因此简单的
use LWP;
就足够了。Assuming you've got LWP installed in your local module directory, put the
BEGIN
block before you try to load LWP (right afteruse strict
).The version number in the original code indicates that it's the minimum required version. Since you've got a newer version and LWP's interface is stable, a simple
use LWP;
will suffice.Host Gator 的解决方案似乎有点复杂。我将使用 lib 模块:
如果您从命令行运行脚本,则有两种方法可以不加更改地运行它。
通过在命令行中键入以下内容将其设置为环境变量:
或将其作为选项添加到 perl 命令
The solution Host Gator seems a bit complicated. I would use the lib module :
If you are running the script from a command line there are two ways you can run it unchanged.
Set it as an environment variable by typing following at command line :
or add it as an option to the perl commaind
或者直接在命令行上使用 perl 选项参数标志 -I 例如。多个目录/项目也带有通用模块
Or directly on the command line using perl option parameter flag -I for eg. multiple directories/projects also with a general module