Passing object from subroutine problem

T

tim

Hello

I'm new here, so, hi!

I've been having the following problem. I have a module which I am
creating to manage sessions for my website. I have the following sub in
it:

#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];
my %session;
tie %session, 'Apache::Session::MySQL', undef,
{ Handle => $dbh,
LockHandle => $dbh};
print "Session session ref = ". \%session . "\n";
return \%session;
}

In the calling routine, I have the following code:

my $sessionref = SessionManager->newSession($dbh);
my %session = %$sessionref;
tied(%session)->delete;

The first two lines seem to work ok, but the 3rd gives this error:

Can't call method "delete" on an undefined value at ./tester line 31.

Bizarely, it seems I can access the object in other ways from the same
context, as the following lines work as expected:

print "session id = $session{_session_id}\n";
print "testing session ref = " . $sessionref . "\n";

(i.e. session id is printed)

I'd really appreciate any comments or suggestions on why the above may
be occuring.

Many thanks

Tim.
 
P

Paul Lalli

tim said:
I'm new here, so, hi!

Welcome! Please make sure you read the Posting Guidelines for this
group (posted here twice a week) to learn valuable tips to maximize the
potential usefulness of posting here...
I've been having the following problem. I have a module which I am
creating to manage sessions for my website. I have the following sub in
it:

#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Always always always use warnings when devleoping Perl code. This
should have told you:
Scalar value @_[1] better written as $_[1]
my %session;
tie %session, 'Apache::Session::MySQL', undef,
{ Handle => $dbh,
LockHandle => $dbh};
print "Session session ref = ". \%session . "\n";
return \%session;
}

In the calling routine, I have the following code:

my $sessionref = SessionManager->newSession($dbh);
my %session = %$sessionref;

%session is a brand new hash, simply populated with the values that are
currently in the variable %$sessionref. It is in all other ways
unrelated to $sessionref. It is not tied to any object of the
Apache::Session::MySQL class.
tied(%session)->delete;

The first two lines seem to work ok, but the 3rd gives this error:

Can't call method "delete" on an undefined value at ./tester line 31.

Because %session is not a tied variable, tied() returns undef.
Bizarely, it seems I can access the object in other ways from the same
context, as the following lines work as expected:

print "session id = $session{_session_id}\n";

_session_id is one of the keys you copied over when you created the new
hash, so yes, that works.
print "testing session ref = " . $sessionref . "\n";

(i.e. session id is printed)

$sessionref is a reference to the original tied variable, so yes, that
works.
I'd really appreciate any comments or suggestions on why the above may
be occuring.

Copying a hash does not copy the tied nature of the variable.

Paul Lalli
 
T

tim

Thanks Paul

Whoops- didn't notice that warnings were off. I'll make sure they're on
before posting next time!!

Tim

Paul said:
tim said:
I'm new here, so, hi!

Welcome! Please make sure you read the Posting Guidelines for this
group (posted here twice a week) to learn valuable tips to maximize the
potential usefulness of posting here...
I've been having the following problem. I have a module which I am
creating to manage sessions for my website. I have the following sub in
it:

#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Always always always use warnings when devleoping Perl code. This
should have told you:
Scalar value @_[1] better written as $_[1]
my %session;
tie %session, 'Apache::Session::MySQL', undef,
{ Handle => $dbh,
LockHandle => $dbh};
print "Session session ref = ". \%session . "\n";
return \%session;
}

In the calling routine, I have the following code:

my $sessionref = SessionManager->newSession($dbh);
my %session = %$sessionref;

%session is a brand new hash, simply populated with the values that are
currently in the variable %$sessionref. It is in all other ways
unrelated to $sessionref. It is not tied to any object of the
Apache::Session::MySQL class.
tied(%session)->delete;

The first two lines seem to work ok, but the 3rd gives this error:

Can't call method "delete" on an undefined value at ./tester line 31.

Because %session is not a tied variable, tied() returns undef.
Bizarely, it seems I can access the object in other ways from the same
context, as the following lines work as expected:

print "session id = $session{_session_id}\n";

_session_id is one of the keys you copied over when you created the new
hash, so yes, that works.
print "testing session ref = " . $sessionref . "\n";

(i.e. session id is printed)

$sessionref is a reference to the original tied variable, so yes, that
works.
I'd really appreciate any comments or suggestions on why the above may
be occuring.

Copying a hash does not copy the tied nature of the variable.

Paul Lalli
 
T

Tad McClellan

Paul Lalli said:
tim wrote:
#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Always always always use warnings when devleoping Perl code. This
should have told you:
Scalar value @_[1] better written as $_[1]


And that accesses the 2nd argument. What about the 1st argument?

my $dbh = $_[0];

but a much better way to handle subroutine arguments is:

my($dbh) = @_; # don't forget the parenthesis

That makes it very easy to add an additional argument:

my($dbh, $max_value) = @_;
 
M

Mirco Wahab

Thus spoke tim (on 2006-09-27 14:08):
I'm new here, so, hi!
Great!

#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Shouldn't the @_[1], which is
a array slice containing the
*second* argument of the caller,
read: $_[0] ... ?
my $sessionref = SessionManager->newSession($dbh);
my %session = %$sessionref;
tied(%session)->delete;

Here you drop a single argument to your function ...

(The return will be a ref to empty hash or sth.)

Regards

Mirco
 
P

Paul Lalli

Mirco said:
Thus spoke tim (on 2006-09-27 14:08):
I'm new here, so, hi!
Great!

#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Shouldn't the @_[1], which is
a array slice containing the
*second* argument of the caller,
read: $_[0] ... ?

No. Please take a few moments to learn about method calls in Perl:
perldoc perlmod
Here you drop a single argument to your function ...

Yes, but Perl passed an additional first argument - the object itself.

Paul Lalli
 
M

Mirco Wahab

Thus spoke Paul Lalli (on 2006-09-27 15:02):
Mirco said:
Thus spoke tim (on 2006-09-27 14:08):
#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Shouldn't the @_[1], which is
a array slice containing the
*second* argument of the caller,
read: $_[0] ... ?

No. Please take a few moments to learn about method calls in Perl:
perldoc perlmod

Uhmm, you are correct, he actually used
object call syntax, sorry.
Yes, but Perl passed an additional first argument - the object itself.

About time for me to get used to "Perl Objects", I guess.

Thanks & Regards

Mirco
 
P

Paul Lalli

Tad said:
Paul Lalli said:
tim wrote:
#takes an active db handle, returns a new session object.
sub newSession {
my $dbh = @_[1];

Always always always use warnings when devleoping Perl code. This
should have told you:
Scalar value @_[1] better written as $_[1]


And that accesses the 2nd argument. What about the 1st argument?

my $dbh = $_[0];

newSession is a class method, so the first argument is the name of the
class. The argument that the user passed into the method is indeed the
2nd argument within the method.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top