Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Simple autoflush module

by RMGir (Prior)
on Aug 12, 2002 at 18:48 UTC ( #189582=sourcecode: print w/ replies, xml ) Need Help??

Category: Miscellaneous
Author/Contact Info RMGir
Description: I do a lot of one-liners, and often want Autoflush turned on.

But typing

perl -ne'BEGIN{$|=1}...'
for each is a bit of a pain, so I wrote this so I can do
perl -MAF -e'...'
instead.

I could always do

perl -ne'$|=1;...'
but that's probably very inefficient.

The name is short, since calling it "Acme::Autoflush" would pretty well defeat the purpose.

package AF;

# that's all there is to this module; we just
# want STDOUT to autoflush.  If a different
# handle is selected when we get here, well, 
# too bad =)

# As merlyn pointed out, I didn't need a BEGIN here...
$|=1;

# USAGE: perl -MAF -ne'whatever-you-want'
# or
#        perl -MAF -pe'whatever'

# I don't even need this 1, since the result of the 
# assignment is 1, but I think it's cleaner to leave this
# in
1;

Comment on Simple autoflush module
Download Code
•Re: Simple autoflush module
by merlyn (Sage) on Aug 12, 2002 at 19:10 UTC
      Good point, thanks.

      It's not a big problem, though, since I care more about saving keystrokes on the commandline than in the module.
      --
      Mike

Re: Simple autoflush module
by robobunny (Friar) on Aug 12, 2002 at 22:16 UTC
    you could save yet more keystrokes by making a small wrapper script.
    #!/usr/bin/perl $cmd = join(';', @ARGV); system(qq{/usr/bin/perl -ne'\$|=1;$cmd'});
    then name it something like 'nperl' and all you have to do is:
    echo "werd up" | nperl s/e/o/g print
    word up
      Nice one, thanks!

      My 1-liners tend to be a bit more complex than this would handle (in fact, they're often 2 and 1/2 liners:), but that'd be very handy for the short cases.
      --
      Mike

Re: Simple autoflush module
by BrowserUk (Pope) on Aug 13, 2002 at 04:50 UTC

    Probably not all you want, but name this

    #!perl -w $|++; eval <>;

    as p1.pl (and under Win32 at least) you can type:

    C:\test>p1 print "hello world!" hello world! C:\test>
    Gets around all (most?) of the brain-dead CLP quoting problems.
      Very cool, thanks!

      This thread is turning into lazy perl central :)
      --
      Mike

Back to Code Catacombs

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-09-22 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (182 votes), past polls