http://qs321.pair.com?node_id=827070


in reply to Re: It's the little things...
in thread It's the little things...

Curiously, for me, it was almost the opposite problem.

I thought it was neat that I could just assign a list variable to a scalar variable and get its length.

Or that I could use the list variable in list context and Perl would dutifully use the number ofelements in the list variable.

And that's where I started to get into trouble...I often (too often!) THOUGHT I in scalar context where I was actually in list context! And that always seemd to bite me in really big, complicated scripts where I had to spend hours and hours tracing the source of the error!

In the process, of course, I learned a LOT about contexts and when and where they apply.

But what I also learned was that using scalar() ALWAYS preventd that problem.

So, for me, the extra effort of typing the scalar() around the list variable has become second nature and saves me a lot of headaches...plus it makes my intentions clear and obvious to me and that those that have to maintain my code.

But that's just what works for me...it certainly is not elegant. YMMV.

ack Albuquerque, NM

Replies are listed 'Best First'.
Re^3: It's the little things...
by webfiend (Vicar) on Mar 06, 2010 at 00:36 UTC

    I think what saves me is that I don't dig too deep into context - just enough to save myself a little effort. I've seen folks get into these bizarre mazes of context that completely lose me, but I avoid those in my own code.