A little background followed by a bunch of questions.

I am working on old shoddy code written by someone else who was an 
administrator not a developer, yikes, yeh I know bad place to be, but 
hey they are paying me for this.

I have a function that currently returns an array when it should be 
returning a hash, this function is called in a lot of places so 
rewriting the code where it is called is not really feasible. 
Unfortunately we also have new code that is being created that will need 
to use this function as well before we can rewrite the whole damn system 
(which is also in planning thank <insert higher being here>).

As an intermediary I happened upon wantarray as a way to change the 
implementation of the function without changing the current design spec, 
aka still allow the function to return an array for the old calls but 
allow new code to use a better method.  After going through a couple of 
iterations I ended up returning an array when wantarray is true and a 
hash reference when it is false.

Question 1:  Is there a way to differentiate the return context between 
"array context" and "hash context" from what I have seen there is just 
one big "list context"??

This last point leads to,

Question 2:  How does Perl (and can I therefore some how do it too) know 
that sometimes a hash in list context is a hash, and sometimes it isn't?

i.e. ($return, $return1, %hash) = &sub_that_returns_2_vars_and_a_hash

This maintains the %hash correctly from when the sub returns a hash 
(naturally after popping off the first key/value pair).

Question 3:  What defines "void context"??  For instance, I realize that 
calling a sub by itself, aka,

&call_sub_like_this;

is "void" context, however I am unclear what the following is (or how 
Perl determines it, though this is less important for now if it will 
make my head hurt),

if ( &call_sub_like_this ) {
        do something cool here....
}

Is this void because I am not assigning the value to something? It seems 
that the value is needed to determine the true or falseness of the 
conditional, if this is not void does is it automagically scalar 
context, or does Perl decide based on other factors, aka what I am 
returning or the operator preceding the func call, etc.?

Question 4:  Is anyone still reading this far down?? ;-)

Question 5:  In my function I need to check the return context multiple 
times, is it better form or more efficient to continue to call wantarray 
several times (5-8) or to assign the value of wantarray early in the 
function and then test the variable that holds its value?  Obviously 
this may be stylistic, but I am still curious.

Question 6: I realize the amount of info I have provided is somewhat 
limited, but is the best way to go in the situation as I explained 
above, where I pass a hashref when in scalar context and an array in 
list context, or is there a better solution, possibly one that will 
allow me to pass a hash directly rather than the reference?  (personally 
I don't mind working with references but some of our "junior" team 
members are still intimidated by them).

Any other comments are appreciated......

Thanks,

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to