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

remember perl one-liners

by tinita (Parson)
on Dec 11, 2005 at 05:17 UTC ( #515794=CUFP: print w/ replies, xml ) Need Help??

inspired by What one-liners do people actually use? i wrote a little utility for bash:

addhist.pl:

#!/usr/bin/perl use strict; use warnings; my @lines = <>; pop @lines; my $hist = join '', @lines; my $history = "$ENV{HOME}/.oneliners"; $hist =~ s/\A\s*\d+\s+//; open my $fh, ">>", $history or die $!; print $fh $hist; close $fh;
# put these in your .bashrc alias addhist="history 2 | addhist.pl" alias readhist="history -r ~/.oneliners" $ perl -wle'something short and useful' $ addhist [hours or days later...] $ readhist $ !! # will get you your last saved oneliner from above
i can't get this to work with "oneliners" that have more than one line, though...

Comment on remember perl one-liners
Select or Download Code
Re: remember perl one-liners
by vek (Prior) on Dec 11, 2005 at 09:13 UTC

    i can't get this to work with "oneliners" that have more than one line, though...

    Oneliners with more than one line, by definition, cease to be oneliners.

    -- vek --
      that's why i put the word in quotes, yeah.

      for the record: i can't get this to work with two-to-ten-liners (history breaks each line up into one single command). any bash experts around?

Re: remember perl one-liners
by l3v3l (Monk) on Dec 14, 2005 at 20:15 UTC
    From the bash man page :
    The cmdhist shell option, if enabled, causes the shell to attempt to save each line of a multi-line command in the same history entry, adding semicolons where necessary to preserve syntactic correctness. The lithist shell option causes the shell to save the command with embedded newlines instead of semicolons. See the description of the shopt builtin ... under SHELL BUILTIN COMMANDS for information on setting and unsetting shell options.
    dug into it and here is the gist:
    optional feature of Bash history filtering that isn't always enabled by default in distributions is cmdhist. This determines whether multi -line commands are stored in the history as a single command (on) or not (off; default). To enable this feature, you would type shopt -s cmdhist To disable this feature, you would type shopt -u cmdhist
    not sure if this fixes the prob ... update: script and alias(s) work with cygwin as well
      thanks for that pointer, that sounded like it could solve the problem, but i didn't...

      i think the lithist option means a multiline command like:

      $ for i in *.pl do echo $i done $ <arrow up> $ for i in *.pl ; do echo $i; done $ shopt -s lithist $ <arrow up> $ for i in *.pl do echo $i done
      the problem appears if command arguments itself have newlines, an easy example is
      $ echo " word "
      so it would work if one calls perl with multiple -e args:
      $ perl -wle'print 1;' \ -e 'print 2;'
      but that's ugly, isn't it? =)

      update: but i still think it should work because if i type in a multiline perl command and type arrow-up immediately after that i get all my lines like i typed them in. just storing in the history doesn't seem to work.

        Ahh ... makes sense - thought that would help : I tried the echo experiment with cmdhist set and it put echo into history file as one (multi-line) entry - now reading it back in ... is a whole other beast ... this does not do exactly what you require but it does allow multi-line liners to be read back in as one line: after the first $hist substitution in addhist.pl :
        $hist =~ s/\s+/ /gm;
        so I can now add (from history output):
        ... 601 perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/(aaaaa(?:(?:(?!aaaaa).)*))/s)->ex +plain' 602 addhist
        as:
        perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/(aa +aaa(?:(?:(?!aaaaa).)*))/s)->explain'
        to my ~/.oneliners file = thank you again for posting this script - I am a big fan of oneliners and this script is a big help in keeping them around longer than my histsize ...
Re: remember perl one-liners
by chanio (Priest) on Dec 23, 2005 at 06:12 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2014-08-29 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls