Posts Tagged by Perl
refactoring in Go – rather pleasant actually…
| 12-Feb-2013 | Posted by Sonia Hamilton under Golang, Perl, Python, Ruby, Vim |
I’ve just finished refactoring a large Go program, and the process was rather…. pleasant.
Static typing catches all those obscure errors I wouldn’t think about in a scripting language (Python, Perl, Ruby, etc). My process is:
- type :make in vim (I have a dummy Makefile in my Go project just for vim)
- vim jumps cursor to error (vim quickfix list)
- “oh, I shouldn’t do that” – fix (type type type)
- start again
Finish rather sooner than expected, run tests, smile in knowledge program is working properly.
Update
To quickly setup the make command for Go, type this in a Vim window:
:setlocal makeprg=go\ build\ \.
Or even better configure vim via your ~/.vimrc, for example:
autocmd BufRead *_test.go setlocal makeprg=go\ test\ \. autocmd BufRead *.go setlocal makeprg=go\ test\ \./..
Thanks Martin for the comment!
Perl – debug fork() with no X
| 03-Aug-2012 | Posted by Sonia Hamilton under Perl |
Here’s how to debug a Perl program that’s running remotely on a server that doesn’t have X. It’s pretty simple, these are just notes for me…
You’ve ssh’d into a remote server (headless), to debug a perl script that forks:
$SIG{CHLD} = sub {
while (1) {
my $nino = waitpid(-1, WNOHANG);
last if $nino <= 0;
if ($?) {
&log("ERROR: Child process $nino exited with $?");
}
$children--;
}
# let's fork!
my $pid = fork();
if (not defined $pid) {
&log("ERROR: fork(): resources not available");
}
elsif ($pid > 0) {
# parent
$children++;
next;
}
else {
# child
$SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
do_stuff($arg1, $arg2);
exit;
}
But when you start debugging, you get output like this (which tells you what to do):
######### Forked, but do not know how to create a new TTY. ######### Since two debuggers fight for the same TTY, input is severely entangled. I know how to switch the output to a different window in xterms, OS/2 consoles, and Mac OS X Terminal.app only. For a manual switch, put the name of the created TTY in $DB::fork_TTY, or define a function DB::get_fork_TTY() returning this. On UNIX-like systems one can get the name of a TTY for the given window by typing tty, and disconnect the shell from TTY by sleep 1000000.
The easiest solution is to install a minimal X on the target server, then reconnect via ssh, check your DISPLAY variable, then start up the debugger in xterm.
server% sudo aptitude install xinit xterm client% ssh -X server # reconnect, to setup DISPLAY server% env | grep -i disp DISPLAY=localhost:11.0 # DISPLAY is good server% xterm & # an xterm should display locally server% perl -d foo.pl # start debugging
After forking, one or more additional xterms will display for each child, allowing you to debug.
Lisp and Python
| 27-Mar-2011 | Posted by Sonia Hamilton under Lisp, Perl, Python, Ruby |
I’m bored, so I’m going to learn Lisp. Why am I bored? I hurt my knee at BJJ and have to take a week or two off training :-( Fortunately it’s not too bad and I’ll be back getting beaten up (and beating up) soon!
I came across this fun looking book that inspired me – Land of Lisp – I started reading it in Dymocks and couldn’t put it down. But years ago I’d read Paul Graham’s blog and recently Joel Spolsky on The Perils of Java Schools. I was really surprised to learn that university Computer Science departments have stopped teaching pointers and recursion because they teach Java or .NET. I remember the joy :twisted: in 2nd year Comp Sci of watching half the class being weeded out by Data Structures, then another half being weeded out the following semester by Functional Programming.
And here’s a post by Peter Christensen on who uses Lisp.
Oh yeh, I’m also learning Python – it seems the sanest general purpose scripting language around. Ruby is great and I love it, but it’s often not available (try telling Bank X or Phone Company Y that you’re going to install Ruby on several hundred of their production Solaris servers, see what sort of reaction you get). And Perl is unmaintainable line noise and shell scripts are tedious.
What kicked this all off? This great SlashDot post on why Expensify doesn’t hire .NET programmers :-)
PS I like Paul Graham’s Tweet on the book: “Turns out the border between genius and insanity is a pretty cheery place”.
Source keychain credentials in Perl
| 09-Jun-2009 | Posted by Sonia Hamilton under Cron, Perl, Ssh |
I use keychain for securely caching my ssh key credentials when running scripts from cron.
Here’s how to use keychain with Perl scripts:
Create a wrapper script: source ~/.keychain/hostname-sh run_perl_program.pl
To run a one-off command do:
system("source ~/.keychain/hostname-sh; cmd");
Perl – @INC
| 02-Jun-2009 | Posted by Sonia Hamilton under Perl |
@INC – like $PATH for do, require, use
Display existing @INC:
- perl -V | tail
- perl -le ‘print for @INC’
Modify @INC:
- in a script: unshift @INC, “/home/sonia/lib/perl”
- better: use lib ‘/home/sonia/lib/perl/’;
- for shell: export PERL5LIB=/home/sonia/lib/perl
- onetime at command line: perl -I/home/sonia/lib/perl foo
Also:
- perldoc perlrun
- perldoc lib
- Intermediate Perl

Recent Comments
<<EOF>>was eaten...cat <>~/.vi...