I think that its ok to quote small pieces of a book, so long as you provide proper refences back to that book. If you want to give a longer example, keep in mind that most programming books provide the code online in addition to in the text, so you can include that url instead of posting lots of code. For example, the Perl Cookbook provides some code online at
O'Reilly, although I doubt that's all of it. Another example is Lincoln Stein's
Official Guide to Programming with CGI.pm for which
John Wiley & Sons, inc. maintain a web page with all the code examples.