Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing object from subroutine problem

Reply
Thread Tools

Passing object from subroutine problem

 
 
tim
Guest
Posts: n/a
 
      09-27-2006
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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-27-2006
tim wrote:

> 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

 
Reply With Quote
 
 
 
 
tim
Guest
Posts: n/a
 
      09-27-2006
Thanks Paul

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

Tim

Paul Lalli wrote:
> tim wrote:
>
> > 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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-27-2006
Paul Lalli <(E-Mail Removed)> wrote:
> 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) = @_;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      09-27-2006
Thus spoke tim (on 2006-09-27 14:0:

> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-27-2006
Mirco Wahab wrote:
> Thus spoke tim (on 2006-09-27 14:0:
>
> > 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

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

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


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

Paul Lalli

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      09-27-2006
Thus spoke Paul Lalli (on 2006-09-27 15:02):
> Mirco Wahab wrote:
>> Thus spoke tim (on 2006-09-27 14:0:
>> > #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.

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

>
> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-27-2006
Tad McClellan wrote:
> Paul Lalli <(E-Mail Removed)> wrote:
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
Passing subroutine 'Sender' and 'e' (EventArgs) manually? mj.redfox.mj@gmail.com ASP .Net 2 02-20-2007 08:54 AM
Problem with passing array to subroutine Ben Bullock Perl Misc 11 05-26-2006 03:43 AM
Passing a list as an arg to a subroutine Mark Drummond Perl 2 03-09-2006 07:50 AM
Options for passing Hash to a subroutine. chung.ley@amd.com Perl 6 04-04-2005 04:47 AM



Advertisments