Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Re: Re: Dereferencing an array of hash references

by mfriedman (Monk)
on Jun 26, 2002 at 20:12 UTC ( #177507=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Dereferencing an array of hash references
in thread Dereferencing an array of hash references

In that case, you need to properly dereference what you're getting back from db_retrieve_records.

my @loop = db_retrieve_records();

should be

my @loop = @{ db_retrieve_records() };

Also, you need to learn the difference between my and local.

Fixed a typo.

Replies are listed 'Best First'.
Re: Re: Re: Re: Dereferencing an array of hash references
by fuzzyping (Chaplain) on Jun 26, 2002 at 20:25 UTC
    First, I'd like to thank you very much for solving the deref. That did in fact solve the problem with my data structure. However, I would like to know why you're inferring that I don't know the difference between my and local. Perhaps I've used an instance improperly, but I do know the differences and (supposedly) when to use them. I've locallized my scope where possible, and, where impossible, used local to allow scope to continue on to called subroutines.

    Would you mind elaborating? I'm not claiming to be right, but I sure don't know unless you give me a bit more detail as to your criticism.

    Either way, thanks again! :-)

      Sure. What caught my attention was this:

      sub mail { local $self = shift; local $dbh = $self->param('dbh'); my $q = $self->query(); local $session = $q->param('session'); .... }

      Since you've got a variable called $self, I'm assuming this is an object method. What you're doing here is creating a local version of the package variable $self. So if you had a package global called $self (which you probably shouldn't) you'd be overriding it. That is generally not the behavior that you want in a method. (or any sub, really.) You want a lexical variable called $self, one which is unrelated to any other $self in any other lexical block. The reason I assumed you did not know the difference is because I saw in that code lots of locals mixed in with mys in places that did not make a lot of sense. I would write that code as:

      sub mail { my $self = shift; my $dbh = $self->param('dbh'); my $q = $self->query(); my $session = $q->param('session'); }

      Here's an excellent document explaining when to use each: Coping With Scoping.

        I should probably mention that my script is actually a package using the CGI::Application base module. That alone will probably clarify some of the scope issues you're referring to.

        I've read Coping With Scoping before, and I think I have a relatively adequate understanding of the differences. Of course, I know I'm not perfect (or anything resembling it), so it's quite possible I've made some errors in my scoping. Unfortunately, I generally use the "code-n-patch" method to my code... write some code, test it, fix, write some more code, etc. This usually means that I start off with a lexical value and only create a local version of the variable when it's necessary to allow scoping across to another sub. Make sense? Broken ideology? Probably. ;-)

        I've uploaded the entire script to my scratchpad. If you 've got the time and/or inclination, I would greatly appreciate your criticisms.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177507]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-06-13 16:26 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (57 votes). Check out past polls.