在简单 Perl 教程中添加模块的包含路径

发布于 2024-09-05 03:23:17 字数 1649 浏览 2 评论 0原文

我正在尝试做一个简单的教程,但开始时遇到困难。我的问题似乎是安装和获取模块的正确路径。

**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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-09-12 03:23:17

假设您已将 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 after use 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.

站稳脚跟 2024-09-12 03:23:17

Host Gator 的解决方案似乎有点复杂。我将使用 lib 模块:

use strict ;
use lib '/home/d********n/perl' ;
use LWP ;

如果您从命令行运行脚本,则有两种方法可以不加更改地运行它。

通过在命令行中键入以下内容将其设置为环境变量:

export PERL5LIB=/home/d********n/perl 
myscript.pl

或将其作为选项添加到 perl 命令

perl -I/home/d********n/perl myscript.pl

The solution Host Gator seems a bit complicated. I would use the lib module :

use strict ;
use lib '/home/d********n/perl' ;
use LWP ;

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 :

export PERL5LIB=/home/d********n/perl 
myscript.pl

or add it as an option to the perl commaind

perl -I/home/d********n/perl myscript.pl
十秒萌定你 2024-09-12 03:23:17

或者直接在命令行上使用 perl 选项参数标志 -I 例如。多个目录/项目也带有通用模块

perl -I'../project/lib' -I'../otherProject/lib' -I'lib' -M'Test::Doctest'  -e run lib/MyOwnModule.pm

Or directly on the command line using perl option parameter flag -I for eg. multiple directories/projects also with a general module

perl -I'../project/lib' -I'../otherProject/lib' -I'lib' -M'Test::Doctest'  -e run lib/MyOwnModule.pm
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文