Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I generate a relative simple HTML page I want to open in the standard Browser used in a Windows computer. I generally can open a html file stored locally with the following

my $filename = "/abc.html"; system("start file://$filename");

I would like to do the same without saving the HTMl code I've generated in a html file. I have the HTML code in a variable. Do you see any possibility to do it? The only thing I've come up is to use a temporary file (File::Temp). Thanks.

Replies are listed 'Best First'.
Re: Generate HTML and open it in standard Browser (Windows)
by Corion (Patriarch) on Jun 11, 2014 at 14:43 UTC

    In addition to your approach of a tempfile, HTML::Display (and HTML::Display) implement some other approaches. But mostly, it falls back to writing to a tempfile and launching the browser with that.

Re: Generate HTML and open it in standard Browser (Windows)
by roboticus (Chancellor) on Jun 11, 2014 at 16:42 UTC

    I've not tried it, but there are several modules that provide a minimal HTTP server in your application. If you go that route, you can not only direct your browser to your HTML page, but you could also do some more complex applications that take form input. (The only module that comes to mind is Plack, but that's just because the odd name stuck with me. I've not tried any of those modules and am not recommending it over anything else, just offering it as an example.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Generate HTML and open it in standard Browser (Windows)
by Anonymous Monk on Jun 11, 2014 at 14:45 UTC

    In theory you might be able to use data URIs (e.g. data:text/html,<h1>Hello World</h1>), but I've found temporary files to be much more robust. In that respect, File::Temp is probably the best way to go, you might find its UNLINK option helpful (unless the browser is supposed to persist after the Perl script terminates).

    Starting a local webserver might be another option, but depending on your application it might be a little overkill. But take a look at HTTP::Server::Simple and Mojolicious::Lite anyway.

Re: Generate HTML and open it in standard Browser (Windows)
by LanX (Saint) on Jun 11, 2014 at 14:45 UTC
    Why? It depends...

    There are ways with add-ons but they are overly complicated.

    The classic way is to open the browser with a localhost link and port and to serve the file... :)

    Cheers Rolf

    (addicted to the Perl Programming Language)

Re: Generate HTML and open it in standard Browser (Windows)
by neilwatson (Priest) on Jun 11, 2014 at 14:42 UTC
    I think this is a issue of the external command. Will it accept HTML from standard input? I don't think it will.

    Neil Watson
    watson-wilson.ca

Re: Generate HTML and open it in standard Browser (Windows)
by Discipulus (Canon) on Jun 12, 2014 at 07:22 UTC
    The simplest solution is a temp file: you can use directly the ENV entry for this.

    See below the Jenda suggested macro to open perldoc in the browser:

    pdoc=perldoc -o html -T -w index $* > %TEMP%\perldoc_temp.html && star +t %TEMP%\perldoc_temp.html
    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Generate HTML and open it in standard Browser (Windows)
by locked_user sundialsvc4 (Abbot) on Jun 11, 2014 at 16:30 UTC

    The reason why the “open” strategy works is that, for this particular operating system, the .html file extension is understood as meaning that one should launch a web browser and pass that file to it.   As far as I know, that browser is either going to have to have the address of a file, or a URL to go to.   You can create a local web-server in Perl, opening it with a URL such as localhost::8080, say, but you will have to have (all of) that.   Thus, perhaps in your case “saving it to a randomly-named file” and doing what you’re doing now is a pretty good choice.   I’m just not sure that the alternatives, such as they are, would be in any way worth the bother in this case.