[Catalyst] About output in a browser
Carl Franks
fireartist at gmail.com
Wed Aug 29 15:37:38 GMT 2007
On 28/08/2007, lanas <lanas at securenet.net> wrote:
> Hi folks,
>
> In understanding how Catalyst work and how to work with , I'm making
> very simple stuff, like having a model that only returns a directory
> listing. I found something curious before starting to make a view.
> Every line of the directory listing, obtained with 'ls -l', will be
> chomped if I do not put a print "String !" just before it in the
> model, like this:
>
> prints chomped lines, which results in unstructured output in the
> browser:
>
> MyApp01/lib/MyApp01/Model/MyApp01Model.pm
>
> sub getFiles()
> {
> my ( $self, $c ) = @_;
> @{$c->stash->{files}} = `ls -l /home/user/`;
> }
>
> prints neatly, every line of the directory listing on a separate line
> in the browser:
>
> sub getFiles()
> {
> my ( $self, $c ) = @_;
> print "getFiles() !";
> @{$c->stash->{files}} = `ls -l /home/frodo/`;
> }
>
> 'getFiles()!' will also get printed in the browser, but I presume this
> is OK.
>
> The output is in the following. There are no view defined at the
> moment.
>
> MyApp01/lib/MyApp01/Controller/Root.pm
>
> sub end : ActionClass('RenderView')
> {
> my ( $self, $c ) = @_;
>
> if (defined $c->stash->{files}) {
> $c->response->body( "@{$c->stash->{files}}" )
> }
> else {
> $c->response->body("NO DATA !");
> }
> }
>
> Can someone tell me why the output of the directory listing is not the
> same due to, apparently, the poresence of a print statement ?
Are you setting this anywhere...?
$c->res->content_type('text/plain');
If not, I think if you view the page source in the browser, you'll see
that the linebreaks are there - but the browser is ignoring them
because it's treating it as HTML.
If you change your output line to:
$c->response->body( join "<br>", @{$c->stash->{files}} )
then I think you'll find it looks as you expect.
(Not that I'm recommending this as a solution - I definitely recommend
using a templating View for most common uses).
If you run the "scripts/myapp_test.pl" file from a command line, you
can easily see the exact output that would be sent to your browser.
Carl
More information about the Catalyst
mailing list