S
Stuart Kendrick
hi,
i'm realizing that i don't understand the distinction between list and
scalar context as thoroughly as i would like. in the examples below,
Program 1.0 delivers a different $answer than do Programs 2.x [and
Programs 2.x all deliver the same $answer.]
in Program 2.x, i understand that i'm using the "for" operator as an
alias for the "foreach" operator ... and that "foreach" surrounds
$element with a list context (whereas, in Program 1.0, $element is
surrounded in a scalar context.) i can vaguely see that perhaps
complex_function knows the context surrounding $element ... and that
it produces different results, depending on $element's context.
[complex_function produces a reference to an array containing a
blessed reference to a four element array ... that's what $answer
becomes. it is a wrapper around the snmpbulkwalk command within the
SNMP module.]
i don't understand why complex_function cares ... i wrote
complex_function, after all. but hey, i'm willing to buy the idea
that it does. so now, i want to force Program 2.0 to place $element
in a scalar context, rather than in a list context. because it turns
out that when complex_function runs in Programs 2.x, i don't get the
output i want; whereas when complex_function runs in Program 1.0, i
get my desired output.
but i don't know how to do that, at least not successfully. you can
see my various, unsuccessful, efforts in Programs 2.1 - 2.n.
insights appreciated.
--sk
Stuart Kendrick
FHCRC
Program 1.0:
$element = 5;
$answer = complex_function($element);
Program 2.0:
$array[0] = 5;
for $element (@array) {
$answer = complex_function($element);
}
Program 2.1:
$array[0] = 5;
for $element (@array) {
$element = 5;
$answer = complex_function($element);
}
Program 2.2:
$array[0] = 5;
for $element (@array) {
$element = scalar $element;
$answer = complex_function($element);
}
Program 2.3:
$array[0] = 5;
for $element (@array) {
$answer = complex_function(scalar $element);
}
i'm realizing that i don't understand the distinction between list and
scalar context as thoroughly as i would like. in the examples below,
Program 1.0 delivers a different $answer than do Programs 2.x [and
Programs 2.x all deliver the same $answer.]
in Program 2.x, i understand that i'm using the "for" operator as an
alias for the "foreach" operator ... and that "foreach" surrounds
$element with a list context (whereas, in Program 1.0, $element is
surrounded in a scalar context.) i can vaguely see that perhaps
complex_function knows the context surrounding $element ... and that
it produces different results, depending on $element's context.
[complex_function produces a reference to an array containing a
blessed reference to a four element array ... that's what $answer
becomes. it is a wrapper around the snmpbulkwalk command within the
SNMP module.]
i don't understand why complex_function cares ... i wrote
complex_function, after all. but hey, i'm willing to buy the idea
that it does. so now, i want to force Program 2.0 to place $element
in a scalar context, rather than in a list context. because it turns
out that when complex_function runs in Programs 2.x, i don't get the
output i want; whereas when complex_function runs in Program 1.0, i
get my desired output.
but i don't know how to do that, at least not successfully. you can
see my various, unsuccessful, efforts in Programs 2.1 - 2.n.
insights appreciated.
--sk
Stuart Kendrick
FHCRC
Program 1.0:
$element = 5;
$answer = complex_function($element);
Program 2.0:
$array[0] = 5;
for $element (@array) {
$answer = complex_function($element);
}
Program 2.1:
$array[0] = 5;
for $element (@array) {
$element = 5;
$answer = complex_function($element);
}
Program 2.2:
$array[0] = 5;
for $element (@array) {
$element = scalar $element;
$answer = complex_function($element);
}
Program 2.3:
$array[0] = 5;
for $element (@array) {
$answer = complex_function(scalar $element);
}