我写了Perl module
,因为我使用了class and objects
。
所以我为类创建了对象并访问它的方法。
例如:
{
package sample;
sub Bless
{
my $Class = shift;
my $Name = shift;
bless \$Name, $Class;
}
}
{
package test;
our @ISA = qw(sample);
sub Print
{
my $Name = shift;
print "Hi, I'm $$Name & This is for testing\n";
}
}
my $My_Obj = test->Bless('Ganapathy');
$My_Obj->Print;
对于上述声明,该程序已正常运行。 当我执行它时,它给出了这样的输出,
Hi, I'm Ganapathy & This is for testing
但是,如果我使用::
这样的对象访问该方法,
$My_Obj::Print;
它没有工作,它会抛出如下错误,
Useless use of a variable in void context at /home/ganapathy/trainee_2015/perl/inter_perl/chap_13/Object_Doubt.pl line 46.
Name "My_Obj::Print" used only once: possible typo at /home/ganapathy/trainee_2015/perl/inter_perl/chap_13/Object_Doubt.pl line 46.
为什么我不能这样访问,请任何人帮助我。 感谢。
答案 0 :(得分:2)
在$My_Obj::Print
中,您要求包Print
My_Obj
您尚未分配或使用其值,所以基本上您只是提到标量变量的名称,并且您收到消息
在void上下文中无用的变量
你想做什么? $My_Obj
显然是一个对象,您已成功使用Print
调用$My_Obj->Print
方法。你为什么要以不同的方式去做?