fun with cat

No, not the feline kind.

I was reading a good book the other day (Hacking: The Art of Exploitation) when I ran into something that I hadn't thought about before:

cat foo - | program

Such a simple little thing, and immediately I want "Oh!  How come I didn't make that connection before?"  It makes perfect sense, first it shoves the contents of foo to program and then hands control back to the user.  How utterly useful at times.

It got me to thinking, could I get away with:

cat - - foo | program

And sure enough, that works too!  Utility, I think there might be a use or two, but so far the ones I've come up with are sort of contrived.

And finally, an alternate method:

perl -e 'stuff(); while(<>) { print;} ; other_stuff()' | program

Also works quite sufficiently (not that it would be any surprise.

Now why hadn't I thought of this before?  I'm not sure, but now I have and it's added to my toolkit.