Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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'...'

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...

# 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
Replies are listed 'Best First'.
Re: Simple autoflush module
by BrowserUk (Patriarch) on Aug 13, 2002 at 04:50 UTC

    Probably not all you want, but name this

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

    as (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 :)

•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.

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2023-02-01 02:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found