相关疑难解决方法(0)

为什么这个程序有效?我试图创建一个语法错误

我在Windows 7上运行ActiveState的32位ActivePerl 5.14.2.我想用Git预提交钩子来检测正在检查的语法错误的程序.(不知怎的,我只是设法做了这么糟糕的提交.)所以作为一个测试程序,我随机记下了这个:

use strict;
use warnings;

Syntax error!

exit 0;
Run Code Online (Sandbox Code Playgroud)

但是,它在没有警告的情况下编译和执行,退出时errorlevel为零.这个有效的语法怎么样?

perl

487
推荐指数
5
解决办法
2万
查看次数

Perl:如何在多重继承中调用特定方法?

我在perl中有一个包,它使用另外两个包作为它的基础.

Parent1:

package Parent1;

use strict;
use warnings;

sub foo
{
    my $self = shift;
    print ("\n Foo from Parent 1 ");
    $self->baz();
}

sub baz
{
    my $self = shift;
    print ("\n Baz from Parent 1 ");
}
1;
Run Code Online (Sandbox Code Playgroud)

家长2:

package Parent2;

use strict;
use warnings;

sub foo
{
    my $self = shift;
    print ("\n Foo from Parent 2 ");
    $self->baz();
}

sub baz
{
    my $self = shift;
    print ("\n Baz from Parent 2 ");
}
1;
Run Code Online (Sandbox Code Playgroud)

子:这使用上面的两个父包. …

perl inheritance package subroutine

7
推荐指数
2
解决办法
521
查看次数

使用 WWW::Mechanize 进行内存泄漏

我在 Perl 中有这个脚本"Out of memory",运行几分钟后出现错误。我看不到任何循环引用,我无法弄清楚为什么会发生这种情况。

use feature 'say';
use WWW::Mechanize;
use HTML::TreeBuilder::XPath;
use utf8;

$url = "some url";

my $mech = new WWW::Mechanize;
$mech->get($url);
my $html = HTML::TreeBuilder::XPath->new_from_content($mech->content);
my $html2;

do { 
    for $item ($html->findnodes('//li[@class="dataset-item"]'))
    {
        my $title = $item->findvalue('normalize-space(.//a[2])');
        next unless $title =~ /environmental impact statement/i;        
        my $link = $item->findvalue('.//a[2]/@href');
        $mech->get($link);
        $html2 = HTML::TreeBuilder::XPath->new_from_content($mech->content);
        my @pdflinks = $html2->findvalues('//a[@title="Go to external URL"]/@href');
        my $date = $html2->findvalue('//tr[th="Date Created"]/td');
        for $pdflink (@pdflinks)
        {
            next unless $pdflink =~ /\.pdf$/;
            $mech->get($pdflink);
            $mech->save_content($filename …
Run Code Online (Sandbox Code Playgroud)

perl out-of-memory www-mechanize

4
推荐指数
1
解决办法
154
查看次数

如何在Perl中的getter方法中返回两个值?

use strict;
use warnings;

package LineSegment;

sub new
{       
        my $class = shift; 
        my ($ax, $ay, $bx, $by) = @_;
        my $self = {"ax"=>$ax,
                    "ay"=>$ay,
                    "bx"=>$bx,
                    "by"=>$by,
                   }; 
        bless ($self, $class);
        return $self;
}
sub getA{
        #Issue on get A
        my $self = shift;
        return ($self->{ax}, $self->{ay});
}
sub getB{
        #Issue on get B
        my $self = @_;
        return ($self->{bx}, $self->{by});
}
sub setA{
        #Can print correct value. Is the return statement where it goes wrong?
        my($self, $ax, $ay) = …
Run Code Online (Sandbox Code Playgroud)

perl

2
推荐指数
1
解决办法
160
查看次数

在Perl OOP中继承抽象方法时找不到方法错误

我有一个从超类调用方法的子类.超类中的方法使用在超类中定义为抽象(不是真正抽象)但在子类中实现的方法.

例如:

package BaseClass;

sub new
{

}
sub method1 {

    return someAbstractMethod();
}



sub someAbtsractMethod
{
     die "oops, this is an abstract method that should " . 
         "be implemented in a subclass" ;
}
1;

package SubClass;

sub new
{

}

sub someAbtsractMethod
{
     print "now we implement the asbtract method";
}
1;
Run Code Online (Sandbox Code Playgroud)

现在我做的时候:

$sub = new SubClass();
$sub->method1();
Run Code Online (Sandbox Code Playgroud)

...它调用抽象消息,我得到指定的错误消息.如果我从超类中取出抽象方法并将实现留在子类中,它就不会识别该方法并且我得到子程序抽象方法未找到错误.

oop perl inheritance abstract

1
推荐指数
1
解决办法
1704
查看次数

在构造函数中输入数组 - Perl

我目前正在Perl中创建一个如下所示的类:

sub new{
   $class = shift;
   $self = {
     #Member Variables
     server => shift,
     type => shift,
     domains => shift
   };
   bless $self, $class;
   return $self;
}
Run Code Online (Sandbox Code Playgroud)

我希望域是一个包含多个变量的数组.使用shift时,如何区分数组和正常变量?我试图像这样输入数组:

 my $var = new class("server",1,(1,2,3));
 my $var = new class("server",1,[1,2,3]);
Run Code Online (Sandbox Code Playgroud)

当尝试迭代数组时,这些都不起作用:

 for $i ($self->{domains}){
     print "$i\n";
 }
Run Code Online (Sandbox Code Playgroud)

arrays perl constructor

1
推荐指数
1
解决办法
167
查看次数

Perl模块,接受列表并创建对象

我正在研究大学问题(在Perl中).我们正在创建模块,我需要编写一个简单的模块"get/set四个属性的方法:lastname,firstname,full_name和一个也是person对象的子列表".

我想我已经失去了它,但它是那些也是抛弃我的人物对象的孩子.我想模块需要接受一个列表,然后创建一个对象列表?Python是我的核心语言,所以这个让我失望.get/set方法工作正常.有任何想法吗?

我的模块在这里......

#!/usr/bin/perl

package Person;

sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
    };
    bless $self, $class;
    return $self;
}
sub setFirstName {
    my ( $self, $firstName ) = @_;
    $self->{_firstName} = $firstName if defined($firstName);
    return $self->{_firstName};
}

sub getFirstName {
    my( $self ) = @_;
    return $self->{_firstName};
}

sub setLastName {
    my ( $self, $lastName ) = @_;
    $self->{_lastName} = $lastName if defined($lastName);
    return $self->{_lastName};
}

sub getLastName { …
Run Code Online (Sandbox Code Playgroud)

oop perl module package

1
推荐指数
2
解决办法
132
查看次数