Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using object fields in a class method

Status
Not open for further replies.

Buddyholly83

IS-IT--Management
Aug 7, 2005
23
GB
I have created an instance of SendServiceTest in my Main perl app and then call a method of SendServiceTest on the object like so:
Code:
my $message = new SendServiceTest
(	
	$username,
	$password,
	$account
);
$message -> sendMessageTest($uri, $recipient, $originator, $body, $type, $validityPeriod);

I then want this method to use the 3 fields of the object plus additional values for calling another method(FormPostTest) in FormPostClass. How do i supply these object values? Below is my SendServiceTest class that creates the FormPostClass object and calls the FormPostTest method (the method i want to pass object values to):

Code:
package SendServiceTest;
use FormPostClass;
use strict;

# Class constructor
sub new
{
	my ($class, $username, $password, $account) = @_;
	my $self = 
	{
		username 	=> $username,
		password 	=> $password,
		account		=> $account
	};
	bless ($self, $class);
	return $self;
}

# Send the text message
sub sendMessageTest
{
	my ($object, $uri, $recipient, $originator, $body, $type, $validityPeriod) = @_;
	
	my $post = new FormPostClass();
	$post -> formPostTest($object->{username}, $object->{password}, $object->{account},
										$uri, $recipient, $originator, $body, $type, $validityPeriod);
	
}
 
So what's the problem?
Does it not work?
If not, what goes wrong? What effect do you see?

Trojan.
 
BTW:
Can I suggest that you always put hash keys in single quotes?
Many a weird bug has appeared because someone uses a bareword key like "length" and wonders why the compiler blows up!


Trojan.
 
Basically the first 3 values i am passing into formPostTest were passing as some sort of hash values?! Putting single quotes around the hash keys solved the problem:

Code:
	my $response = $post -> formPostTest
	(
		'$object -> {username}', '$object -> {password}', '$object -> {account}', 
		$uri, $recipient,	$originator, $body, $type, $validityPeriod
	);

Thanks, TrojanWarBlade :)
 
Ha! Don't wanna sound too much like an idiot but it infact doesn't work. Essentially I need to access the 'username' field of object to pass it to the formPostTest method. I can create a new variable and store the value of the object's field:
my $username = $object -> {username};
but for some reason giving the method $object -> {username} as the field value doesn't work. There is no error but it isn't passing the value of the username field - it is passing some sort of hash reference.
 
You have SPACES in your dereferencing!
Change this:
Code:
'$object -> {username}'
to this:
Code:
$object->{'username'}
And do the others in the same way.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top