In Perl, how does readline assign to $_ in a loop condition but not elsewhere? -


how readline implemented in perl?

question why readline sets $_ if readline used in loop condition such as:

while(<>) {   #here $_ set   print; } 

on contrary, if do

<>; print;  #$_ not set here 

it not print anything?

how implemented? how function know used in loop condition statement? or built-in behavior designed way?

in case, there's nothing special implementation of readline. never sets $_. instead, there's special case in perl compiler examines condition of while loop , rewrites conditions internally.

for example, while (<>) {} gets rewritten into

while (defined($_ = <argv>)) {     (); } 

you can see perl -mo=deparse -e 'while (<>) {}'.

this documented under i/o operators in perlop:

ordinarily must assign returned value variable, there 1 situation automatic assignment happens. if , if input symbol thing inside conditional of while statement (even if disguised for(;;) loop), value automatically assigned global variable $_, destroying whatever there previously.

it's mentioned in loop control & loops in perlsyn.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -