On Fri, Jan 14, 2000 at 10:48:16PM -0800, Bruce Van Allen wrote: > Let me see if I can state this correctly: A few more things to note... > A. Values > The values of concern here are of two kinds in Perl (I'm not saying 'types' > because that means something different): 'scalar' and 'list' (which mean, > in effect, 'singular' and 'plural'). A scalar value may be an integer, > floating point number, exponential expression, string literal, constant, > etc., as well as a reference to any of these or anything else that Perl > lets us make references to (including lists). A list value is an ordered > series of zero or more values separated by commas; parentheses might or > might not be required to make sure the series of values are treated as a > list by functions and operators. You cannot make a reference to a list. However, you can make a reference to an anonymous array, which may be what you meant. > C. Context > The Perl contexts of concern here are 'scalar'and 'list' (there are other > contexts, such as boolean). Scalar context expects a single value. List > context (before called array context) expects zero or more values > structured as a list. Boolean is a special kind of scalar context. The other kinds of scalar context are numeric, string, and void. > Context is established by the, er, context in which a value is being used, > which may be set by Perl operators, built-in functions, and syntactical > structures. Array variables can tell if they are in list or scalar context, > yielding lists in list context and a single value in scalar context. > Well-written subroutines will also Do the Right Thing (and this was the > admirable goal of the original poster). > > Context setting moves from left to right in a statement. [Is this always > true?] So, in the statements > my $var = &some_sub; > my ($var) = &some_sub; > nothing different happens to $var (it gets scoped), but the first puts > &some_sub into scalar context and the latter puts it into list context due > to the parentheses. As you said above, context is established by the context in which a value is used. This applies to expressions as well. For most operators, context of the operands is passed from outside in or is specified by the operator. Assignment is the exception, determining the context of its RHS based on its LHS. For example: [scalar] + [scalar] # addition: both operands in scalar context, # regardless of addition's context $s = [scalar] # assignment: RHS in appropriate context @a = [list] # based on LHS $s = [scalar] || [scalar] # logical-or: left operand in scalar context @a = [scalar] || [list] # right operand in same context as logical-or > I think another reason I continue to mix up the terms 'array' and 'list' is > that for the most part we _can_ use arrays and lists the same way. E.g.: > > @ary = ('a', 'b', 'c'); > $j = $ary[2] ; # $j gets the value 'c'. > > $j = ('a', 'b', 'c')[2]; # $j gets the value 'c'. On the other hand, you can't use any of the array operators (push(), shift(), splice(), et al.) on a list, you can't take a reference to a list, and you can't count the items in a list without iterating over it or assigning it to an array. Ronald # ===== Want to unsubscribe from this list? # ===== Send mail with body "unsubscribe" to macperl-request@macperl.org