Details for the stateful behaviour for operators such as match (ie. m//g
), stat (ie. stat _
) and range (ie. //..//
) are in the documentation. However is there a 'listing' of all operators or functions that exhibit stateful behaviour? The ones that come to mind are:
#ARGV/File/dir/glob: The current line/position is remembered
say while <>; #Read line by line by Line from ARGV files
say while <$fh>; #Read line by line by line from file handle
say while defined($_ = readdir($dh)); #Read an entry at a time from dir handle
say while <*>; #Read an entry at a time from file glob in current dir
say while <{a,b,c},{1,2}>; #Print combination glob one at a time ie (a1,b1,c1,a2,b2,c2)
#Regex: Global modifier/list context remembers previous matches
say while m/$re/g; #Print matches one at a time
my $_="hello"; say /hello/g; say /hello/gc; #Only prints hello once. Stateful continuation from last match position
#stat
stat _; #Returns stat array state from previous stat "filename";
#range: State remembers if the first condition is met
perl -n -e 'print if 1..10' myfile.txt; #Bistable flipflop state. Print lines 1 to 10
perl -n -e 'print if /startmatch/../endmatch/' myfile.text; #print lines between matches
#state variable: make your own
sub { state $myStateVar;}; #Your own state variable
Any info would be great. Thanks
question from:
https://stackoverflow.com/questions/65648641/is-there-a-succinct-list-of-all-perl-stateful-operators-and-modifiers 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…