Do you know where your variables are? | |
PerlMonks |
$| pre and post increment and deincrement behaviorby bioMan (Beadle) |
on Nov 30, 2005 at 19:24 UTC ( [id://513060]=perlquestion: print w/replies, xml ) | Need Help?? |
bioMan has asked for the wisdom of the Perl Monks concerning the following question: A question about elegant ways to split a sequence into groups of identical characters and a response by Roy Johnson using the idiom --$| with grep peaked my interest. I asked a follow-up question about the use of the idiom and was directed to Perl Idoms Explained -$|++, but this didn't really answer my question. So I would like to explore this idiom a little further. The original question asked how to split a string like 'xx556xx' into groups of identical characters e.g. xx, 55, 6, xx. Roy Johnson proposed the idiom --$| with grep as a possible solution. I have used the idiom $|++ in scripts written for Windows to force fflush after every print statement. So I wondered if ++$| could have been used with grep to solve the problem above. My code for looking at $| with incrementing or deincrementing with either pre or post implementation is shown below.
The results are: autoflush = 0 , pre-deincrement Only the pre-deincrement works although the post-deincrement returns a list of the unique characters from the sequence. The list xx,x,55,5,6,6,xx,x is the list returned by m/((.)\2*)/g so pre- and post-incrementation don't appear to be doing anything other than returning that list from the match statement. I thought autoflush could only have values of 0 or 1 and am confussed by the results obtained. I understand that incrementing zero will return one, but what does deincrementing zero in $| return? Does $| get reset to the default value after each list element is examined by grep? The pre- and post-increment results suggest this is not the case. It appears I'm going to have to be much more careful about how I use $|. Update Thanks for the responses. They really helped. I was just playing with Data::Dumper, which also helped me understand how $| sets and resets with various applications of $|++ and $|--. It looks like using increment and deincrement on $| can allow for some pretty neat effects, but like a firecracker can blow up in your face if you're not careful :-).Mike
Back to
Seekers of Perl Wisdom
|
|