Showing posts with label hacks. Show all posts
Showing posts with label hacks. Show all posts

Friday, October 27, 2017

Perl Hack: perlbrew libs

The libs feature of perlbrew is one I don't see used very often. At least, not by the developers I currently work with and have worked with in the past.

Sometimes I want to run a piece of code against the core libraries and only the core libraries. Sometimes I wrap a script up with Carton and want to verify that a base install + Carton can run my script. And sometimes I just want a place to install anything and everything from CPAN, play with new versions' features, etc...

This is where the libs feature comes in handy.

I have three sets of perl 5.20.3 libs:

$ perlbrew list
  perl-5.20.3
  perl-5.20.3@carton
* perl-5.20.3@dev

99% of my time is spent on the "dev" lib, where I install anything I want. The "perl-5.20.3" is just a base installation of 5.20.3. And the "carton" lib is just a base 5.20.3 installation with only Carton installed. And if I ever break the "carton" or "dev" libs, they're easily recreated from the base installation.

Tuesday, August 29, 2017

Serving the Current Directory over SSL

Recently at work, we needed to setup a dummy HTTPS server just as an endpoint that needed to do... something. Nothing specific. Just something that did SSL/TLS and returned a 200 response. Immediately I thought of python's builtin SimpleHTTPServer, which can be used to serve the current directory:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

And away it goes. But to put SSL into it, more code is needed, but there are examples around.

I wondered how easily (or not) I could do it with Perl and a Plack server.

First, I needed the following dependencies installed:

  1. Plack::App::Directory. This comes with the standard Plack distribution, but it's used to serve a directory listing.
  2. Starman. This is currently the only Plack server that supports SSL, without requiring something like nginx in front of it. A little disappointing, but not a big deal.
  3. IO::Socket::SSL. To do the SSL stuff. Requires OpenSSL.

These can either be managed by Carton, or you can just install them with cpanm.

$ cpanm Plack Starman IO::Socket::SSL

Next, I need to generate a dummy SSL certificate.

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

Now I can run the server:

$ plackup -s Starman --listen :8000 \
  --enable-ssl --ssl-cert server.crt --ssl-key server.key \
  -MPlack::App::Directory -e'Plack::App::Directory->new(root=>".")'
2017/08/29-12:27:01 Starman::Server (type Net::Server::PreFork) starting! pid(38015)
Resolved [*]:8000 to [0.0.0.0]:8000, IPv4
Binding to SSL port 8000 on host 0.0.0.0 with IPv4
Setting gid to "20 20 20 504 401 12 61 79 80 81 98 33 100 204 395 398 399"
Starman: Accepting connections at https://*:8000/

... and the directory listing is available via https://locahost:8000/

It's not as simple as Python's SimpleHTTPServer to get going, but it works!

Friday, October 30, 2015

Perl Hack: Modifying a List

One of my favorite Perl hacks, shown to me by a work colleague a few years ago, is for modifying a list. The hack is actually fairly pretty:

s/^ +//g, s/ +$//g for @list;

For very simple substitutions, I think this is more expressive and easier to understand than the usual foreach loop, or a map.

Friday, August 28, 2015

Shell Productivity Hack

I got really sick of iTerm2 (and other terminals) not giving me an easy key-combo to move forward or backward a word at a time; having to hold down the left- or right-arrow key, for what feels like forever, to fix a typo in the middle of a really long one-liner (or a MySQL query, as was often the case) was becoming way too annoying, especially when I do it multiple times a day. So a few months ago I added this little nugget to my ~/.inputrc:

"\e[1;9C": forward-word
"\e[1;9D": backward-word

This lets me press alt-left or alt-right to move word-by-word rather than character-by-character. And now I can't live without it.

Wednesday, November 6, 2013

My Favorite Shell Productivity Hack

This is, by far, my favorite productivity hack for Linux shells, which I stole from a guy I used to work with. In ~/.inputrc :

"\e[A": history-search-backward
"\e[B": history-search-forward

That's it.

What does it do? Well, remember a few hours ago when you scp'd a file from/to some server, but either can't remember or just can't be bothered typing out the full command again? Type, 'scp ' in your shell and start pressing your up arrow and it'll go through all of your past uses of scp. You could just type 'history | grep scp', but when you get used to this hack, you'll start using it all the time.

As a bonus, it also works in, for example, your command-line MySQL client too. And anything else that keeps a command history.

Tuesday, March 12, 2013

Move Around Vim Windows Less Annoyingly

On any given project at work, I will always end up opening 3-4 windows (and even more recently, the NERD Tree sidebar, which is a plugin I recommend everyone get) and I get sick of having to press Ctrl+W and then h, j, k or l to move to the next window. It's one of my most used key combinations and it needed to be shortened.

I wanted to just type Ctrl+[hjkl]. So I added this to my .vimrc:

nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

And that's it. Easier window navigation with a quick vimrc addition!

Sunday, August 2, 2009

Cute Perl Gem to Get the Minimum/Maximum Value

Saw this little nugget on #perl@irc.perl.org the other night. It determines the minimum of two values:

[$b, $a]->[$a <= $b]

It takes advantage of the fact that Perl doesn't have a boolean return type for true or false, so the comparison operators return 1 or 0 for true and false, respectively, which are then used by this code to index the array ref.

To get the maximum of the two values, just flip the operator to >=

Saturday, June 20, 2009

Vim-style Line Numbers in Emacs

Being a heavy Vim user throughout my university degree and now throughout work, I get very used to having the line numbers down the side (with "set numbers" in a .vimrc) as I hack code. To get something like this in Emacs, there is linum-mode.

You can toggle linum-mode for any Emacs buffer with the command M-x linum-mode. Or if you want to set it globally for every file you edit, add this to your .emacs file:
(require 'linum)
(global-linum-mode 1)
linum-mode should come pre-installed with Emacs 22 and beyond. It is also bundled with Aquamacs 1.7 (and maybe earlier...)

Sunday, June 14, 2009

Getting a Perl REPL in Emacs

Although Perl doesn't really have a REPL, you can use Perl's debug mode to obtain something as close to a REPL as you can get without installing extra software with the command perl -de1

So when in Emacs (or XEmacs or Aquamacs or whatever), using the following Emacs commands in sequence will open Perl's debug mode inside a horizontally split window:
C-x 2
C-x o
M-x shell
perl -de1
And away you go.

Wednesday, June 3, 2009

Checking Perl Syntax Within Vim

Sick of switching back and forth between your shell and Vim buffer fixing syntax errors and adding forgotten "use" statements? Running perl -c is a handy command to run on your Perl source files to check for syntax errors. Here's a little key mapping to add to your .vimrc file:
map ,pc <Esc>:! perl -c %<CR>
Now type ,pc (mnemonic: perl -c) in command mode to check the syntax of your Perl source file without having to switch windows. It is a little annoying to have to do it multiple times (if there are many errors) since Vim swaps between the file you're editing and the shell to show the output, but it's definitely a step above manually swapping windows :)