Queue (queue) wrote,

What I learned today

The only time $_ gets automatically set by the line input operator is in the conditional of a while loop. I had seen this shortcut and just assumed that it happened anywhere you used the line input operator. I had run into this not working other places before (like the conditional of an if), and I fixed it by setting another variable to the value returned by the operator. I never knew why it wouldn't work sometimes, though. I never connected it to the while vs. if thing.

In fixing up my Jotto programs this morning, I ran across the problem again. Instead of just blindly fixing it this time, I decided to try to figure it out, since I know a bit more now. And, hey, presto! It's in Programming Perl, right there in black and white. It doesn't say why it works that way, just that it does. I asked on the PerlMonks Chatterbox and was told that it's probably so that $_ doesn't accidentally get clobbered. This makes sense to me. That's good.
  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.