Wiggins D'Anconia wrote: > > 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,
Incorrect. perldoc perlsub [snip] The Perl model for function call and return values is sim ple: all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars. Any arrays or hashes in these call and return lists will collapse, losing their identities--but you may always use pass-by- reference instead to avoid this. Both call and return lists may contain as many or as few scalar elements as you'd like. (Often a function without an explicit return statement is called a subroutine, but there's really no difference from Perl's perspective.) > 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"?? That is correct. > 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). It doesn't know. In the function the array or hash is converted to a list and that list is converted to what ever you like outside the function. > Question 3: What defines "void context"?? For instance, I realize that > calling a sub by itself, aka, > > &call_sub_like_this; > > is "void" context, The wantarray() function returns _three_ values, "true" in a list context, "false" in a scalar context, and undef in a void context. $ perl -e' sub test { print "$_[0]: "; if ( not defined wantarray ) { print "VOID context\n" } elsif ( wantarray ) { print "LIST context\n" } else { print "SCALAR context\n" } return (1,1); } if ( test( 1 ) ) { test( 2 ); @x = test( 3 ); %x = test( 4 ); } ' 1: SCALAR context 2: VOID context 3: LIST context 4: LIST 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? No, this is a scalar (boolean) context. The value that the function returns is used to determine whether or not to run the code in the if (or unless or while or until) block. > 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?? ;-) Yes. > 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. Since the context doesn't change within the function you could just call it once. > 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...... Yes, don't use an ampersand to call a user defined function, they aren't required and define a different behavior then when using them. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]