miércoles, 30 de junio de 2010

Redirect STDOUT to file in Perl 5

Every now and then, I need to write little scripts (or not so little) that output *lots* of lines of text. Those lines belong to a single file, so my common idiom is printing the output to STDOUT, and tell the user to redirect the output to a file if he wants it toasted into a file.

But there's a more elegant way to do it that doesn't rely on shells, and works more consistently. In fact TIMTOWTDI.

One way is fill the code with:

if ($outputInFile) { print $file "foo";}
else{print "foo"}

/me shivers ...

I found a way to do it quite elegantly redirecting a file handler where I'll be printing to STDOUT, using Typeglobs. I'm not too confident managing typeglobs, but it seems to work :)



I asked at #barcelona.pm and alexm (O HAI! president) told me I could think it the other way around, and overwrite STDOUT to an opened filehandle in case I needed the redirection. I didn't know I could handle STDOUT like any other fh. It's nice to know it.



I think Casiano taught me (back in university times) another way to do it (maybe it was using tee, or some IO::Handle funky stuff).

Is there any cpan module that does that kind of Stdout vs file output? I haven't found it, but it MUST be there. Or maybe the code to do it is so small it doesn't make sense writing a module for that

miércoles, 23 de junio de 2010

lunes, 21 de junio de 2010

Kill Buffers Illustrated (emacs)


A side effect of using emacs intensively is that you end with lots (I mean *LOTS*) of open buffers scattered on your emacs.

This is not necessarily a problem (provided you use ido/iswitchb/icicles modes), but it kind of gets on my nerves having 30+ buffers opened knowing that I don't need them anymore. Most of them are temporary buffers like dired-mode ones, or perldoc ones. Like most things in emacs world, someone has already thought of it and there is already a function to do what you want.

For example, scattered dired-mode buffers.

(defun kill-all-dired-buffers()
"Kill all dired buffers."
(interactive)
(save-excursion
(let((count 0))
(dolist(buffer (buffer-list))
(set-buffer buffer)
(when (equal major-mode 'dired-mode)
(setq count (1+ count))
(kill-buffer buffer)))
(message "Killed %i dired buffer(s)." count ))))
And you're done.

great, isn't it?

What if you want to purge some buffers, that have no name in common, nor mode? You can kill them one at a time with kill-buffer (c-x k) but if you want to do it faster, you can use Buffer List buffer.

c-x c-b will open a buffer with a list of all buffers. That's not a normal (Fundamental) buffer, but a 'Buffer Menu' buffer. If you press 'd', the buffer that has the point (cursor) will be marked for deletion. Once you have the list of buffers to kill, just press 'x', and buffers will be killed.

If you press 'x', 3 erc buffers will be killed. Mnd you, once a buffer has been killed, it cannot be unkilled. :)

That's all for now.

viernes, 11 de junio de 2010

I ♡ TITS (TIAS)

There's a (sadly too common) practice consisting of asking about something before having tried anything related to it. I mean, if you're working on nuclear fision, better ask first, but if you're trying a software library, or want to know which advantage gives you using this db vs that other one, just TIAS (or its NSFW version TITS The link is SFW, don't worry). This will give you a fairly good overview of what does this language/lib/anything can do for you, and how easy/difficult is FOR YOU to use it.

erlang
haskell
redis
mongodb
clojure
scheme
python (or this if you have MS Silverlight)

Btw, combo pun for the ones that came to the last FestaFIB '10. A pun on a pun on a pun on "I love ..." T-shirts .

jueves, 10 de junio de 2010

Talk talk talk

A new conference was born 3 months ago. It's called Emerging languages, and it will take place this summer. It'll be full of language designers, talks about innovative programming languages, and *lots* of enlightening ideas (I'm sure). There will be talks about lots of langs all by the language designers. Myself, I'm particularly interested in Io, Ioke , Newspeak, Factor and Clojure.

The bad thing is that it's going to be held in OSCON 2010, so It's a bit far from me... Let's hope there will be recorded talks after summer.

It seems this summer I'm not going to YAPC::EU neither, so I'll have to take my conference dose from somewhere else.

Luckily, there are lots of good talks already recorded .


Erlang and haskell


scheme talks

Google Io sessions

And lots of good papers to read:
ClassicCompScienceTexts

Any other suggestions are very welcome.