Bracing against the wind  

Thursday, May 08, 2008

Perl Round Function

Yes, you can't find this easily on the web. What you get is people telling you to import posix, or do some wacky one-off thing that works half the time. This is the only solution you'll ever need... it should be a builtin.

# round to any decimal place
sub round {
my ($n,$d) = @_;
return int($n/(10**$d) + .5 * ($n <=> 0)) * (10**$d);

Labels: ,

[View/Post Comments] [Digg] [] [Stumble]

Useful Line for Quoting Shell Arguments

Safely pass "@*" to ssh without having to worry about the remote shell re-expanding variables.

qa=`perl -e 'for (@ARGV) {s/(["\\$])/\\\\$1/g; $_="\"$_\"";} print join(" ", @ARGV)' "$@"`
ssh remote-server "cd /some/where; do something; $qa"

Single quote version is safer, but is harder for me to read.

qa=`perl -e "for (@ARGV) {s/'/'\\\\\''/g; \\\$_=\"'\\\$_'\";} print join(' ', @ARGV);" "$@"`

Labels: ,

[View/Post Comments] [Digg] [] [Stumble]

Thursday, December 14, 2006

Running Perl Programs as Services

Taking spur of the moment notes, responses to phone calls, ideas, things to do, etc. is something I prefer to do very rapidly. I never seem to manage to have "task software" installed on a given machine, nor do I regularly have web access. Nor would I want to wait, while in the middle of a thought, for a program to open, or a web page to load. Some people use a pen, but that requires me to lift my hands from the keyboard (slows me down). Plus I can type faster than I can write.

My solution is to create a shortcut to a text file on the Desktop assign a key sequence to it. I press Control-Shift-T and a text file pops open. Then I add some dashes at the top as a "new entry" marker. I jot some notes down and Alt-F-S, Alt-F4. I rarely remember to time/datestamp my entries, so I'd wrote a program to do it for me.

If it looks complicated, that's because I decided to copy and paste some skeleton code from Win32::Daemon. It's terribly easy now to create Windows services out of Perl programs.

The program monitors my two text "note files" and updates them with timestamps within 1 second after being modified. It uses negligible CPU time. Archiving could be done automatically as well, moving 6 month old entries to a backup, but for now, I like to do that by hand.

Labels: ,

[View/Post Comments] [Digg] [] [Stumble]

Friday, September 23, 2005

AIM:Chat with ErgoEst

Created a chatterbot based on Eliza. It's dumb and annoying, but it taught me a bit about Net::OSCAR - which was my real interest. For the evangelists out there, Jabber just seems rediculous. Run a whole server? DBM/DBI like drivers for ever-changing chat protocols are what's really needed.

Anyway. If you use Trillian (love love), GAIM, or any other AIM client, say hello to ErgoEst.

Labels: , ,

[View/Post Comments] [Digg] [] [Stumble]

Tuesday, May 24, 2005

Pure Perl CGI Server

I wrote a rather efficient bare-bones web server that is only capable of serving perl CGI programs. The very cool thing about it is that it's single-threaded, like thttpd.

Since the webserver is written in perl, you're not execve'ing on every hit. And the server itself is very lightweight, so dprof will probably tell you that slowdowns are coming your CGI.

Note: If your perl program ever blocks on IO, don't think about using this server. But you wouldn't do that, would you?

In summary, if you have a perl CGI that's slow under Apache and you need to handle hundreds of CGI requests per second on a cheap box, ppcgid might help.

Let me know if/how you find it useful.

Labels: ,

[View/Post Comments] [Digg] [] [Stumble]

Home | Email me when this weblog updates: | View Archive

(C) 2002 Erik Aronesty/DocumentRoot.Com. Right to copy, without attribution, is given freely to anyone for any reason.

Listed on BlogShares | Bloghop: the best pretty good | Blogarama | Technorati | Blogwise