|Pathologically Eclectic Rubbish Lister|
Toggling between two valuesby pernod (Chaplain)
|on May 05, 2003 at 17:17 UTC||Need Help??|
pernod has asked for the
wisdom of the Perl Monks concerning the following question:
In a recent discussion, a friend told me 'she was glad I was glad', to which I (of course) immediately replied that 'I was glad she was glad I was glad'. One thing led to another (well, not that....) and voila:
This thing takes an integer from the command line and writes a (possibly ridiculously long) string saying 'I am glad you are glad I am glad...' eg.
The point here is the little list that toggles between 'I am' and 'you are'. The above snippet solves this nicely, but I find it noisy. Having read something about closures recently I then came up with the following:
The code does the same (although the result is in norwegian), and I like this better as it results in only one word where I want the toggle. As a downside, the code's "framework" is extremely verbose.
How does one toggle between two values in a simple and elegant way? Not too long-winded? Not too obscure?
As a bonus question, why doesn't ...
... work under strict? It works with $head predeclared, but I'd rather not as one-liners are more neat. It seems to me that a statement like the above would define $head in the preceding line. Does a my statement inside a condition only apply to the following block? Not the logical "preceding block" to which the conditional is linked?
As a side note, I forgot why I named the variable $head. Now it makes sense, though, if only I knew why.