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

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

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


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

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.

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2021-06-25 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (135 votes). Check out past polls.

    Notices?