<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>crossedstreams.com &#187; PERL</title>
	<atom:link href="http://blog.crossedstreams.com/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.crossedstreams.com</link>
	<description>Total protonic reversal!</description>
	<lastBuildDate>Sat, 21 Jan 2012 20:10:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Perl is slower and faster than Java</title>
		<link>http://blog.crossedstreams.com/development/perl-is-slower-and-faster-than-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl-is-slower-and-faster-than-java</link>
		<comments>http://blog.crossedstreams.com/development/perl-is-slower-and-faster-than-java/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 23:39:52 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PERL]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=104</guid>
		<description><![CDATA[Bit of a random one coming up&#8230; I needed to get an measure of the difference in performance between Perl and Java for a simple client application, so I wrote the traditional &#8216;Hello World&#8217; app in both and ran a bunch of executions averaging over the time from start to end of execution. The net [...]]]></description>
			<content:encoded><![CDATA[<p>Bit of a random one coming up&#8230;</p>
<p>I needed to get an measure of the difference in performance between Perl and Java for a simple client application, so I wrote the traditional &#8216;Hello World&#8217; app in both and ran a bunch of executions averaging over the time from start to end of execution. The net result: </p>
<p><strong>Perl is around 34 times faster than Java.</strong></p>
<p>Really? I thought Java was supposed to be fast? In fact, Dhananjay Nene talked about how comparatively fast a selection of languages &#8211; including Java &#8211; were <a href="http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/">on his blog</a>, and cwilbur &#8216;s comments suggest that in that experiment:</p>
<p><strong>Java is around 100 times faster than Perl.</strong></p>
<p>So how can these two conflicting results both be true? I&#8217;m sure you&#8217;ve already figured it out, but I&#8217;m going to tell you anyway.</p>
<p>Java code takes hundreds of milliseconds to start up, because Java code runs in the Java Virtual Machine which needs a little time to get itself ready before your code can run. Once it&#8217;s up, however, you can get performance close to (or even better than, in reality) that of typical C++.</p>
<p>On the other hand, Perl doesn&#8217;t need that environment and so can start faster but trades off more overhead at runtime, meaning slower running performance.</p>
<p>Just goes to show, application performance is another area where there&#8217;s no one right answer &#8211; it&#8217;s about choosing the right tool for the job &#8211; I don&#8217;t think there&#8217;s any way round these performance characteristics without <a href="http://martiansoftware.com/nailgun/index.html">cheating</a>.</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/perl-is-slower-and-faster-than-java/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How do I send files to the Browser?</title>
		<link>http://blog.crossedstreams.com/development/how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser</link>
		<comments>http://blog.crossedstreams.com/development/how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 22:05:09 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PERL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[mp3 doc pdf xls content-disposition content-type http]]></category>

		<guid isPermaLink="false">http://crossedstreams.com/wordpress/?p=6</guid>
		<description><![CDATA[So you&#8217;ve coded up your new webapp. It does some cool stuff and then produces files in a binary format (like .pdf, .doc, .mp3) and you need to deliver these files to the browser. So how do you do that? Random Junk If you just &#8216;include&#8217; the file, or you read the file into your [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve coded up your new webapp. It does some cool stuff and then produces files in a binary format (like .pdf, .doc, .mp3) and you need to deliver these files to the browser. So how do you do that?<span id="more-6"></span></p>
<h3>Random Junk</h3>
<p>If you just &#8216;include&#8217; the file, or you read the file into your script and then print it out to the browser, you just get a jumble of weird characters. The problem is that the file content has been encoded to be read by a particular application and when the browser tries to render it as text, it applies the wrong encoding &#8211; so you get the random characters.</p>
<p>Luckily, it&#8217;s pretty easy to get a browser to understand that the content you&#8217;re sending isn&#8217;t supposed to be rendered like HTML. You just have to let the browser know what kind of data you&#8217;re sending before you send it, and that&#8217;s pretty easy. Setting just one HTTP header, the &#8216;Content-type&#8217; header, is all that&#8217;s needed. Most languages include support for setting a header, like PHP:</p>
<pre class="prettyprint">
header("Content-type: application/pdf");</pre>
<p>Setting this header will make most browsers throw up a dialog with options to run the file with an application, or save the file.</p>
<h3>PHP Example</h3>
<p>Here&#8217;s an example you can run, if you have PHP to hand. You&#8217;ll need a pdf file to send, and you&#8217;ll need to change the filename and path if it&#8217;s not in the same location as the PHP file.</p>
<pre class="prettyprint">
### set a header to tell the browser what kind of file I'm about to send
header("Content-type: application/pdf");
### then read in the .pdf file
$pdfLocation = "my_pdf_file.pdf";
$pdf = file_get_contents($pdfLocation);
### and the file out to the browser
echo $pdf;</pre>
<p>All good so far, but you might be wondering about that &#8216;application/pdf&#8217; bit. It&#8217;s a MIME (Multipurpose Internet Mail Extension) type, a code that provides a key for the applications that will be able to handle encoded data. IANA, the organisation that manages the types provides a reference for all registered types <a href="http://www.iana.org/assignments/media-types/">here</a>.</p>
<h3>A Word on Encoding</h3>
<p>When sending the file content, you&#8217;ll need to make sure that there&#8217;s no character encoding applied, because the data you&#8217;re dealing with isn&#8217;t a set of characters. Swapping stuff that just happens to look like a Windows-style newline for a UNIX-style newline is going to mess things up too. In the examples the file reading and writing is going on in binary mode, or using input/output streams instead of the usually more convenient character based stuff &#8211; to preserve the data from the file exactly as it was in the file.</p>
<p>When the dialog appears to run or save the file, the suggested filename is the name of the PHP script doing the work, not the name of the file &#8211; so another HTTP header that can come in useful is Content-Disposition. Amongst other things, this header lets you specify a filename for the file to be saved &#8211; however &#8211; although I&#8217;ve not had any issues myself, but I did see a blog post on <a href="http://www.hanselman.com/blog/TheContentDispositionSagaControllingTheSuggestedFileNameInTheBrowsersSaveAsDialog.aspx">IE compatibility problems</a>.</p>
<pre class="prettyprint">
header("Content-Disposition: inline; filename=filename.pdf";);</pre>
<p>Well, hope that&#8217;s of some use to you. I&#8217;ll finish up by providing a couple of simple implementations in other languages. The examples should run in an appropriate web environment without any special support.</p>
<h3>PERL example</h3>
<p>No helper method to set the header here, so we have to manually print the header in the right format, including a following blank line.</p>
<pre class="prettyprint">
#!/path/to/perl
# set HTTP header to tell browser what type of data to expect
# in this case, a PDF file
print "Content-type: application/pdf\n\n";
# then open the pdf file
$pdfLocation = "my_pdf_file.pdf";
$pdfBuffer = open(PDF, "&lt;$pdfLocation");
# set to work in binary mode
binmode(PDF);
# read 64k chunks of the source file and write
# them to the output stream
while (read (PDF, $pdfBuffer, 65536)) {
    print $pdfBuffer;
}</pre>
<h3>Java Servlet Example</h3>
<p>It&#8217;s long-winded and untidy to do this in one Servlet class but hey.</p>
<pre class="prettyprint">
package com.crossedstreams.blog.post6;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;
/**
 * Servlet implementation class PdfServlet
 * reads a PDF file on the local filesystem and pushes it out to the browser
 */
public class PdfServlet extends HttpServlet {
    /**
     * Sends a PDF file in response to HTTP GET request
     */
    @Override
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        // set HTTP header to tell browser what type of data to expect
        // in this case, a PDF file
        response.setContentType("application/pdf");
        // and give the browser a filename to save as
        response.setHeader("Content-disposition", "inline; filename=filename.pdf");
        // then open the pdf file
        String pdfLocation = "/path/to/my_pdf_file.pdf";
        FileInputStream pdfFile = new FileInputStream(pdfLocation);
        // get the output stream back to the client
        OutputStream out = response.getOutputStream();
        byte[] pdfBytes = new byte[65536];
        try {
            // read 64k chunks of the source file and write
            // them to the output stream
            while (pdfFile.read(pdfBytes) != -1) {
                out.write(pdfBytes);
            }
            // all done, make sure the data has all been sent
            out.flush();
        } finally {
            // tidy up, even if there were errors
            try {
                pdfFile.close();
                out.close();
            } catch (IOException e) {
                // I have to catch this exception
                // ...not a lot I can do with it though
            }
        }
    }
}</pre>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

