Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

programming questions - passing parameters

Quote Reply
programming questions - passing parameters
Alex,

Is there any good rule of thumb when to use shift vs @_ array assignment in passing parameters?

Is there any place where using one over the other will come back to bite you?

If a reference is passed, it seems best to use 'shift' and you are using the same dataset. But using @_ is more problematic. If you assign the passed values to a list, you are using copies of the passed values. In most cases, this is what you want to do, since trying to operate on the values in @_ can lead to problems.

Looking at the GT module code, both forms are used.

Is it better to "shift" references, but assign variables/data to local variables?

I'm breaking up the routines into modules, hopefully each of which could stand alone.

The first user passed parameter has to be the $PLUGIN_NAME, so the local routine knows what $PLUGIN_CFG to get.

I don't see any mod_perl problems with that... Passed parameters are evaluated each time.



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: programming questions - passing parameters In reply to
Hi,

Don't know how I missed all these questions, these are the ones I typically love answering. =)

1. Well shift is typically slower then accessing $_[0] directly (now this is only important in functions/methods that are going to be called a _lot_ in one request, otherwise it's not an issue).

2. shift will also typically copy the values. So:

sub uppercase_shift {
my $string = shift;
return uc $string;
}

sub uppercase_args {
return uc $_[0];
}

do two different things. The first one copies the string and returns an uppercased one, the second one uppercases what you pass in!

It's important to remember this when dealing with large strings.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: programming questions - passing parameters In reply to
In Reply To:
Don't know how I missed all these questions, these are the ones I typically love answering. =)
These are the fun kind, they force creativity.

You must be having a week like me. I had local hardware problems, that have kept me somewhat out of work for a couple of days, and yesterday, when I went to pick up a new piece of hardware, I took a fall that by all rights should have put me in the hospital for a few day, but only left me banged up (badly). Fortunately I wasn't alone, and everyone is still wondering how I missed hitting my head, and how I was able to stand up only 10 minutes after the event. The older you get, the more it hurts, and the longer it takes to heal.....

PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin