Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: simple regex question

by phenom (Chaplain)
on Nov 26, 2010 at 14:20 UTC ( #873871=note: print w/ replies, xml ) Need Help??


in reply to simple regex question

You could try something like this:

#!/usr/bin/perl use strict; use warnings; $|++; use 5.010; my $before = 'foo'; my $after = 'stuff'; my $pattern = 'blablah'; my $combined = $before . $pattern . $after; my @test = qw( blahblahblah stuffstuffstuff fooblablahstuff fooblahblahstuff onefooblablahstuff ); match($_) for(@test); sub match { my $test = shift; say "$test matches" if( $test =~ /$combined/o ); }


Comment on Re: simple regex question
Download Code
Re^2: simple regex question
by JavaFan (Canon) on Nov 26, 2010 at 15:08 UTC
    $|++
    Why?

    Is there a reason to insert obscure, needless, lines in your program? Considering that all you're are doing is writing newline terminated strings to STDOUT, which already flushes on newlines by default, there isn't much of a reason to disable buffering.

    But if, for some reason, you want to disable buffering, why this syntax? What's wrong with a plain $| = 1;? You do realize that not everyone knows what the result of $|++;$|++;$|--;$|--; is?

      You're absolutely correct. It's in there because it's part of a template I use for everything I write. Just habit, I suppose.

      I also agree that $| = 1 is more legible. But people who know about removing obscure, needless lines from programs will also know what it means. This too is just another habit - which even perlcritic doesn't complain about.

      There are some caveats with buffering on STDOUT. The behavior changes if Perl figures that STDOUT has been redirected to something not a physical console. Consider:
      #!/usr/bin/perl -w use strict; for (0..200) { print "$_ buffered xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"; print STDERR "$_ unbuffered \n"; }
      When run from the command line, STDOUT is indeed unbuffered and you see alternating buffered/unbuffered statements. However for example if I run this in Textpad (my editor on Windows - I think similar to Notepad++) and have it capture the output, STDOUT is buffered. And the first buffered line is output after about 88 lines. I conclude that on my Windows system the buffer is about 8KB. So it depends upon where STDOUT is directed to.

        I'm fully aware of that, and that's why I wrote by default. It's completely irrelevant for the program I was replying to, as it wasn't writing to different filehandles, and then outside of the program merging them again. It was writing to STDOUT, and nothing else. If it was being redirected to a file, you don't need to unbuffer.

        But my beef wasn't so much about the unbuffering. It was about the idiom that should never have been cargo culted into widespread use.

        Yes, and I know who to blame.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://873871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-09-17 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (97 votes), past polls