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

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! :-)

-fuzzyping
  • Comment on Re: Re: Re: Re: Dereferencing an array of hash references

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Dereferencing an array of hash references
by mfriedman (Monk) on Jun 26, 2002 at 22:05 UTC
    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.

      Thanks,
      -fuzzyping