This blog no longer exists.
You will be redirected to something cool.

Saturday, September 17, 2011

Wrapping My Head Around Scalar & List Context

I've been working on learning the various nuances of scalar and list context in Perl. It seems straightforward, but I am worried I'm not getting things 100%. There is a lot of scary talk basically saying if I don't learn it, bad things will happen. This sort of leads me to believe that this might either be difficult stuff or easy to overlook.

In fact, one example which was an array in scalar context output something I didn't expect. I expected the script to spit out the array in a string, but rather it gave me 6, which is the number of elements in the array. I'm pretty sure I know why, but I'll just have to do the homework to see if I'm really grasping this concept.

3 comments:

Context is one of the fundamental ideas of Perl. If you accept it, you're life will be much easier. It's something that people try to ignore because it's not a concept they see in other computer languages, but it's also the thing that makes Perl differently better than other languages.

However, there's no general rule about what will happen in any context. Many things decide for themselves what they want to do. An array in scalar context give the number of elements in the array, but sort() does nothing, and get* functions returns the most interesting item in their list, for instance.

my $count = @array;
my $undefined = sort @array; # undef, always
my $uid = getpwnam($name); # third element of everything it gets

An array in a double-quote context will join it's elements with a space (technically, the value in $") and return that string.

my $string = "@array";

In general, Perl tries to Do What I Mean (DWIM), so in each case, the thing that happens is probably the most common thing that most people would want (which doesn't mean it's what you want).

It seems straightforward, but I am worried I'm not getting things 100%

It *is* straightforward, what isn't straightforward (for someone who is learning this) is understanding function calls in general. This little program (gisted, because retarded blogger does not allow <pre> tags in comments) tries to explain this, by showcasing a stack-trace - that is a nested list of function calls it took to reach a particula "cluck" call (just follow the line numbers and it will all make sense). AFAIK you know about recursion already, so following this "poke under the hood" shouldn't be too hard.

Besides this the program shows why being oblivious to context is problematic. Notice how worker() returns the same thing every time, but the caller interprets this same thing differently. A well written function will dtrt when faced with different contexts, but this is not always the case.


P.S. I am aware there are a lot of things in this program that are rather idiomatic. Keep in mind that you can figure them out later on, the task at hand is understanding why the notion of context even exists.

Thanks for the comments! I appreciate all the help! It'll take a while for the comments to sink in. Seriously, thanks for all your help!