M
macapone
Hi Folks,
My apologies for a somewhat long post.
I have a perl class, call it MyAPI. It exports one function, called
Init(), which simply returns TRUE. The final MyAPI product will export
200+ functions.
Now, consider this test class:
#########################################
#!/usr/local/bin/perl -w
package APITest;
use strict;
use MyAPI;
use Exporter;
use vars qw( @ISA @EXPORT );
@ISA = qw( Exporter );
@EXPORT = qw
(
DoRequest
) ;
my $api;
sub new
{
my $self = {};
bless $self;
$api = MyAPI->new();
return $self;
}
sub DoRequest
{
my ($self) = @_;
my (
$returnval,
$response
) ;
# $api;
eval "\$returnval = \$api\-\>Init();";
if ($@)
{
$response = $@;
}
else
{
$response = "Success!\n";
}
return $response;
}
1;
##################################
.... and a quick test.pl file to try it out:
##################################
use APITest;
my $xmltest = new APITest;
$response = $xmltest->DoRequest();
print $response;
##################################
OK, so the test class has a global variable, $api, which gets
initialized as a new MyAPI in the constructor. Then, in DoRequest, we
have this:
eval "\$returnval = \$api\-\>Init();";
# i.e, run the code '$returnval = $api->Init();'
(Yes, I know you're all wondering why all these hoops. Bear with me,
the code I'm presenting here is a small part of a big project.)
The problem is, when you run the test.pl program, you get:
"Can't call method "Init" on an undefined value at (eval 2) line
1."
Apparantly, $api is undefined within that eval() expression. However,
if you do anything to reference $api directly, before the evel, the
code works and returns "Success!" Note the commented line above the
eval():
# $api;
Just uncommenting that line is sufficient to make the code work.
So, my question is, what is going on inside DoRequest()? Why is $api
apparantly undefined within the eval(), when doing anything to
reference it somehow magically makes it visible? I'm hoping those with
more experience than me in class/variable/scoping issues will see
something obvious here. I'm still new at object-oriented programming
in Perl (been doing old-fashioned procedural stuff with it for
years...)
Thanks in advance!
Michael
My apologies for a somewhat long post.
I have a perl class, call it MyAPI. It exports one function, called
Init(), which simply returns TRUE. The final MyAPI product will export
200+ functions.
Now, consider this test class:
#########################################
#!/usr/local/bin/perl -w
package APITest;
use strict;
use MyAPI;
use Exporter;
use vars qw( @ISA @EXPORT );
@ISA = qw( Exporter );
@EXPORT = qw
(
DoRequest
) ;
my $api;
sub new
{
my $self = {};
bless $self;
$api = MyAPI->new();
return $self;
}
sub DoRequest
{
my ($self) = @_;
my (
$returnval,
$response
) ;
# $api;
eval "\$returnval = \$api\-\>Init();";
if ($@)
{
$response = $@;
}
else
{
$response = "Success!\n";
}
return $response;
}
1;
##################################
.... and a quick test.pl file to try it out:
##################################
use APITest;
my $xmltest = new APITest;
$response = $xmltest->DoRequest();
print $response;
##################################
OK, so the test class has a global variable, $api, which gets
initialized as a new MyAPI in the constructor. Then, in DoRequest, we
have this:
eval "\$returnval = \$api\-\>Init();";
# i.e, run the code '$returnval = $api->Init();'
(Yes, I know you're all wondering why all these hoops. Bear with me,
the code I'm presenting here is a small part of a big project.)
The problem is, when you run the test.pl program, you get:
"Can't call method "Init" on an undefined value at (eval 2) line
1."
Apparantly, $api is undefined within that eval() expression. However,
if you do anything to reference $api directly, before the evel, the
code works and returns "Success!" Note the commented line above the
eval():
# $api;
Just uncommenting that line is sufficient to make the code work.
So, my question is, what is going on inside DoRequest()? Why is $api
apparantly undefined within the eval(), when doing anything to
reference it somehow magically makes it visible? I'm hoping those with
more experience than me in class/variable/scoping issues will see
something obvious here. I'm still new at object-oriented programming
in Perl (been doing old-fashioned procedural stuff with it for
years...)
Thanks in advance!
Michael