Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Contribute a hack to the new "Perl Hacks" book

by Ovid (Cardinal)
on Dec 16, 2005 at 22:50 UTC ( #517396=perlnews: print w/replies, xml ) Need Help??

Nicked from Allison's journal and reposted with permission.

Do you have a shortcut or two you use all the time when you're writing Perl code? I don't mean how you condense your code from 12 lines to 3 characters, but things like the Vim or Emacs incantations you can't live without, or Firefox shortcuts so you can just type "cpan Module::Name" in the URL bar to jump to results. Or, perhaps something written in Perl that's a little off-the-wall but makes your life easier or more fun, like your favorite Perl GUI widget, or a Perl-controlled alarm clock?

If you have an idea like this, or if you just want to debate the rules of the CPAN drinking game, drop us a note at (this will send your message to the list and subscribe you). The best ideas will get added to a book called Perl Hacks.


New address of my CGI Course.

  • Comment on Contribute a hack to the new "Perl Hacks" book

Replies are listed 'Best First'.
Re: Contribute a hack to the new "Perl Hacks" book (filter)
by tye (Sage) on Dec 17, 2005 at 06:04 UTC

    I put this on my scratchpad about a month back:

    Cool vi macro: @p runs Perl code between #! and __END__ lines on any text after the __END__, preserving the perl code. The "let" command to set @p only works in vim, but the macro (drop the quotes and replace \r with ^V^M -- probably by typing ^V^V^V^M) works in any reasonable vi-alike.

    :let @p="L?^#!\r/^__END__\ry''''p!Gperl\rG"

    L goes to the bottom of the current screen so that the next bit is unlikely to fail (which would halt the macro); ?^#! searches for the first line of the 'script'; /^__END__ searches for the end of what needs to be pasted because it won't be output when perl is run; y'' yanks from the __END__ through the #! line; '' jumps back to the __END__; p pastes the perl code (so the original copy above can remain in the editor buffer); !Gperl runs the bottom of the file (the freshly pasted Perl code and the text to be filtered). G goes to the bottom of the results after filtering.

    For example, when processing error logs (which are usually too verbose for any one task), I'll trim the log repeatedly, often reusing parts of saved trimming code (in part because I prefer Perl regexes). Part of the value of this method is if I get such code wrong, I can just "u" (undo), adjust the code, and try again. (But I also often have filters that makes sense to apply more than once.)

    #!/usr/bin/perl -n next if /.../; s/.../.../; s/.../.../; if( /.../ ) { my( $date, $time )= /(\d([-/\d]+\d) (\d([\d:.]+\d)/; my( $hr, $min, $sec, $ms )= split /[:.]/, $time; my $now= $ms/1000 + $sec + 60*( $min + 60*$hr ); if( $Then ) { $sec= $now - $Then; $min= int( $sec / 60 ); $sec -= 60*$min; $_= sprintf "+%d:%06.3f %s", $min, $sec, $_; } $Then= $now; } print; __END__ ...

    - tye        

Re: Contribute a hack to the new "Perl Hacks" book
by hossman (Prior) on Dec 17, 2005 at 01:21 UTC

    I don't really want to subscribe the this list, but I have this little ditty in my .emacs file, and i use it at least once a day...

    ;; ; run perl on the current region, updating the region (defun perl-replace-region (start end) "Apply perl command to region" (interactive "r") (shell-command-on-region start end (read-from-minibuffer "Replace region comma +nd: " '("perl -ple \'$_=eval\'" . 12 )) t t ) (exchange-point-and-mark) )

    I got it from this snippet but added the eval so that i can immediately evaluate simple expressions (usually math equations). Since it's interactive (and since the cursor is placed in just the right spot) it's easy to replace the eval with something else when I need to.

    (Anyone who wants to mail this in should feel free)

Re: Contribute a hack to the new "Perl Hacks" book
by zentara (Archbishop) on Dec 17, 2005 at 12:57 UTC
    Well this is a shortcut I use all the time, to setup a Perl script. I use midnight commander for almost all my work, and it has a "template menu" which you can invoke in the editor to add template code. This one lets you "touch filename" ( I have touch bash-aliased to just 't' :-) ), then hit F4 to edit the empty new file, then F11 to get a selection of templates. Then hit 'p' to insert the basic perl template.

    In /usr/share/mc/

    + y unknown & t r p #!/usr/bin/perl echo "#!/usr/bin/perl use warnings; use strict; " >%b
    I also have extended it to make a basic gtk2 app with the 'h' key
    + y unknown & t r h #!/usr/bin/perl echo "#!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; my \$window = Gtk2::Window->new('toplevel'); \$window->set_title('Z'); \$window ->signal_connect( 'destroy' => \\&delete_event +); \$window->set_border_width(10); \$window->set_size_request(300,200); my \$vbox = Gtk2::VBox->new( FALSE, 6 ); \$window->add(\$vbox); \$vbox->set_border_width(2); my \$hbox= Gtk2::HBox->new( FALSE, 6 ); \$vbox->pack_end(\$hbox,FALSE,FALSE,0); \$hbox->set_border_width(2); my \$frame0 = Gtk2::Frame->new('Controls'); \$vbox->pack_end( \$frame0, FALSE, FALSE, 0 ); \$frame0->set_border_width(3); \$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0 +); my \$hbox0 = Gtk2::HBox->new( FALSE, 6 ); \$frame0->add(\$hbox0); \$hbox0->set_border_width(3); my \$button = Gtk2::Button->new_from_stock('gtk-quit'); \$hbox0->pack_end( \$button, FALSE, FALSE, 0 ); \$button->signal_connect( clicked => \\&delete_event ); \$window->show_all(); Gtk2->main; ##################################### sub delete_event { Gtk2->main_quit; return FALSE; } " >%b

    I'm not really a human, but I play one on earth. flash japh
Re: Contribute a hack to the new "Perl Hacks" book
by gaal (Parson) on Dec 19, 2005 at 13:13 UTC
Re: Contribute a hack to the new "Perl Hacks" book
by cog (Parson) on Dec 21, 2005 at 10:43 UTC
    Is the mailing list still active, or was it all done in a couple of days?

    I tried submitting a first hack, but got no feedback, so I don't know whether to continue... :-\

      The list is still active. Not everyone is getting replies as Allison and chromatic are fairly busy (and not every hack will be accepted, I'm sure). I saw your hack, though. It looks useful (of course, it's not my call whether or not it will be accepted).


      New address of my CGI Course.

        Yes, but I was expecting that it would get me on the list and that I'd receive my own email back... and that didn't happen, so I started wondering...

        Is there another way to subscribe the list? Are there archives I can browse to see what's still missing? (I think I can come up with quite a few, probably)

Re: Contribute a hack to the new "Perl Hacks" book
by jacques (Priest) on Dec 17, 2005 at 00:03 UTC
    I don't like the idea of a book entitled "Perl Hacks". It only perpetuates a common stereotype of Perl and its users.

    Perhaps it should be called "Useful Perl Hacks" or something else that sounds more noble.

    Update: How about?: Killer Perl Hacks...

      That title was chosen because it's part of the O'Reilly Hacks series. It will look nice next to "Google Hacks", "Astronomy Hacks", "Mind Hacks", etc.


      New address of my CGI Course.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2021-01-15 18:28 GMT
Find Nodes?
    Voting Booth?