可以在模块之间传递Perl对象引用吗?

可以在模块之间传递Perl对象引用吗?,perl,class,module,reference,perl-module,Perl,Class,Module,Reference,Perl Module,示例代码: 下午一时 testClass2.pm test.pl 当我尝试调用$testClass1->testRef,$testClass2Ref=undef 如何从父对象传递对象的引用 更新 哦,对不起,我在示例的构造函数中遗漏了字符串 sub new { my($class) = shift; $testClass2Ref = shift; my $self = {name=>'testClass1'}; bless $self, $class;

示例代码:

下午一时 testClass2.pm test.pl 当我尝试调用
$testClass1->testRef
$testClass2Ref=undef

如何从父对象传递对象的引用

更新 哦,对不起,我在示例的构造函数中遗漏了字符串

sub new
{
    my($class) = shift;
    $testClass2Ref = shift;
    my $self    = {name=>'testClass1'};
    bless $self, $class;
    return $self;
}
此测试正在运行,但Eclipse调试器将此变量显示为“undef”


谢谢您的帮助。

除了语法错误之外,您没有使用
严格模式。打开它将显示
$self
未在两个包中声明。通过替换:

bless $self, $class;
与:


一切都按预期进行。

除了语法错误,您没有使用
严格模式。打开它将显示
$self
未在两个包中声明。通过替换:

bless $self, $class;
与:


一切都按预期进行。

修复语法错误后,一切正常

> ./test.pl
> Test 2
你失踪了

my $self = {};
在两种
新方法中

一个有用的工具是

perl -wc testClass1.pm

当您修复语法错误时,它会起作用

> ./test.pl
> Test 2
你失踪了

my $self = {};
在两种
新方法中

一个有用的工具是

perl -wc testClass1.pm
可以在模块之间传递Perl对象引用吗

绝对是

在这个测试脚本中,我创建了两个类,一个用于测试,另一个用于测试。记住,对象只是引用,方法只是子例程;以同样的方式使用它们

#!/usr/bin/env perl

use strict;
use warnings;

package Tester;

sub new {
  my $class = shift;
  my ($other) = @_;

  my $self = { other => $other };
  bless $self, $class;

  return $self;
}

sub examine { 
  my $self = shift; 
  print "I'm holding a: ", ref( $self->{other} ), "\n";
}

package Candidate;

sub new { return bless {}, shift }

package main;

my $candidate = Candidate->new();
my $tester = Tester->new( $candidate );

$tester->examine();
编辑:现在使用更现代的系统(基于)和。这节省了大量样板文件,让您可以专注于希望对象做什么,而不是如何实现它们

#!/usr/bin/env perl

#technically Moose adds strict and warnings, but ...
use strict;
use warnings; 

use MooseX::Declare;
use Method::Signatures::Modifiers;

class Tester {
  has 'other' => ( isa => 'Object', is => 'rw', required => 1 );

  method examine () {
    print "I'm holding a: ", ref( $self->other() ), "\n";
  }
}

class Candidate { }

no MooseX::Declare;

package main;

my $candidate = Candidate->new();
my $tester = Tester->new( other => $candidate );

$tester->examine();
有关更现实的情况,请参见一些较大的模块系统如何传递表示复杂概念的对象。在我的头顶上,物体在整个系统中传递

可以在模块之间传递Perl对象引用吗

绝对是

在这个测试脚本中,我创建了两个类,一个用于测试,另一个用于测试。记住,对象只是引用,方法只是子例程;以同样的方式使用它们

#!/usr/bin/env perl

use strict;
use warnings;

package Tester;

sub new {
  my $class = shift;
  my ($other) = @_;

  my $self = { other => $other };
  bless $self, $class;

  return $self;
}

sub examine { 
  my $self = shift; 
  print "I'm holding a: ", ref( $self->{other} ), "\n";
}

package Candidate;

sub new { return bless {}, shift }

package main;

my $candidate = Candidate->new();
my $tester = Tester->new( $candidate );

$tester->examine();
编辑:现在使用更现代的系统(基于)和。这节省了大量样板文件,让您可以专注于希望对象做什么,而不是如何实现它们

#!/usr/bin/env perl

#technically Moose adds strict and warnings, but ...
use strict;
use warnings; 

use MooseX::Declare;
use Method::Signatures::Modifiers;

class Tester {
  has 'other' => ( isa => 'Object', is => 'rw', required => 1 );

  method examine () {
    print "I'm holding a: ", ref( $self->other() ), "\n";
  }
}

class Candidate { }

no MooseX::Declare;

package main;

my $candidate = Candidate->new();
my $tester = Tester->new( other => $candidate );

$tester->examine();
有关更现实的情况,请参见一些较大的模块系统如何传递表示复杂概念的对象。在我脑海中,对象在整个系统中传递

请修复语法错误。当我在做了必要的最小更改(不平衡的大括号,类结束时的真值)后运行程序时,我在testClass2.pm第7行得到了
不能祝福非参考值。
如果我们无法重现问题,您将很难提供帮助。请检查并修复语法错误。当我在做了必要的最小更改(不平衡的大括号,类结束时的真值)后运行程序时,我在testClass2.pm第7行得到了
不能支持非参考值。
如果我们无法重现问题,您将很难提供帮助。