Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^4: Pivoting 2 dimensional array refsby John M. Dlugosz (Monsignor) |
on May 14, 2011 at 15:43 UTC ( [id://904839]=note: print w/replies, xml ) | Need Help?? |
More verbose doesn't mean easier to read. The separate branches means I have to read through and grasp that the LHS is the same, and then re-arrange it in my mind to the higher-level idea: set LHS to one of two things depending on condition. Writing the more concise form more closely represents the higher-level meaning. That doesn't mean you have to make it overly terse: write it on separate lines: It's no different than using a for loop instead of a while, to put the different parts of the idiom together. For sequential steps, I'll certainly write it on multiple lines with named variables for the intermediate values, rather than cramming it into one huge nested statement. But this is not like that. Spreading it out makes it harder to read. More specialized forms, like the one he used (//), are idiomatic in Perl. open or die. $normal // $blank. You say "optimized idioms". Well, I think we are speaking across purposes. Nesting a bunch of stuff instead of writing separate statements doesn't make it any clearer. But using a well-known idiom gives a higher-level impression of its meaning, what it's used for and the specific nature of the problem it is addressing. If you don't know them yet, sure they are confusing because the writer doesn't need to comment in detail what those issues and solved problems are — the use of the idiom says it all. But once you learn why a particular twist of the keyboard is written that way, maybe read a whole essay about it, then you just follow that example and consider the matter settled. As for cocnating the "defined or", you might want to start with the regular logical || and its low-precedence counterpart or. Look how it's different from C, in that the result of the expression isn't just "true" but the value from the first true argument. They are used tersely like footnotes: if that normal way didn't work out, use this instead. Or as control-flow, picking the first one that applies. If I have a parameter $x, and the caller can leave off the parameter and mean the default value of 42, you can write, in the body of the function $x//42. Or you can set it if it's not passed, with $x //= 42;. The pulling of the data from the matrix with missing values is the same: give me xy, but if that's not available, use blank instead. The code you learned for opening files uses this kind of control flow too:
In Section
Cool Uses for Perl
|
|