3

Bit long question, because AFAIK Poet/Mason2 isn't the very often used framework - so I'm trying to be detailed.

Two years ago I asked a question how to make Mason2 utf8 clean. As far as i know, here isn't much new in Mason/Poet in this field - and unfortunately today I meet another problem. Simple test case:

$ poet new my   #create new poet application
$ cd my

Override some methods, allowing to use utf8 in the components:

add to ./lib/My/Mason/Compilation.pm

override 'output_class_header' => sub {
    return join("\n",
        super(), qq(
            use utf8;
            use Encode qw(encode decode);
        )
    );
};

The above adds to each compiled Mason component the use utf8....

Also need encode the output from Mason (Plack need bytes), so in: ./lib/My/Mason/Request.pm

override 'run' => sub {
    my($self, $path, $args) = @_;
    my $result = super();
    $result->output( Encode::encode('UTF-8', $result->output()) );
    return $result;
};

Now, can create a component such page.mc for example with a content:

% sub { uc($_[0]) } {{
a quick brown fox jumps over the lazy dog.
διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα.
árvíztűrő tükörfúrógép.
dość gróźb fuzją, klnę, pych i małżeństw!
эх, чужак, общий съём цен шляп (юфть) – вдрызг!
kŕdeľ šťastných ďatľov učí pri ústí váhu mĺkveho koňa obhrýzať kôru a žrať čerstvé mäso.
zwölf boxkämpfer jagen viktor quer über den großen sylter deich.
% }}

After running a poet app bin/run.pl you can go to: http://0:5000/page and will get a correct content.

A QUICK BROWN FOX JUMPS OVER THE LAZY DOG. ΔΙΑΦΥΛΆΞΤΕ ΓΕΝΙΚΆ ΤΗ ΖΩΉ ΣΑΣ ΑΠΌ ΒΑΘΕΙΆ ΨΥΧΙΚΆ ΤΡΑΎΜΑΤΑ. ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP. DOŚĆ GRÓŹB FUZJĄ, KLNĘ, PYCH I MAŁŻEŃSTW! ЭХ, ЧУЖАК, ОБЩИЙ СЪЁМ ЦЕН ШЛЯП (ЮФТЬ) – ВДРЫЗГ! KŔDEĽ ŠŤASTNÝCH ĎATĽOV UČÍ PRI ÚSTÍ VÁHU MĹKVEHO KOŇA OBHRÝZAŤ KÔRU A ŽRAŤ ČERSTVÉ MÄSO. ZWÖLF BOXKÄMPFER JAGEN VIKTOR QUER ÜBER DEN GROSSEN SYLTER DEICH.

But when create another component, say go.mc with a content

% $m->go('/page');

the internal redirect (the go method) somewhat mess up the content and will produce:

A QUICK BROWN FOX JUMPS OVER THE LAZY DOG. ÎÎÎΦΥÎÎÎΤΠÎÎÎÎÎΠΤΠÎΩΠΣÎΣ ÎÎ Î ÎÎÎÎÎΠΨΥΧÎÎΠΤΡÎÎÎÎΤÎ. ÃRVÃZTŰRÅ TÃKÃRFÃRÃGÃP. DOÅÄ GRÃŹB FUZJÄ, KLNÄ, PYCH I MAÅÅ»EÅSTW! ЭХ, ЧУÐÐÐ, ÐÐЩÐРСЪÐРЦÐРШÐЯР(ЮФТЬ) â ÐÐРЫÐÐ! KÅDEĽ ŠŤASTNÃCH ÄATĽOV UÄà PRI ÃSTà VÃHU MĹKVEHO KOÅA OBHRÃZAŤ KÃRU A ŽRAŤ ÄERSTVà MÃSO. ZWÃLF BOXKÃMPFER JAGEN VIKTOR QUER ÃBER DEN GROSSEN SYLTER DEICH.

Strange, the $m->visit() works correctly. So, somewhere in Poet/Mason is need do something to get correct output for the go method.

Could anyone help?

Community
  • 1
  • 1
kobame
  • 5,766
  • 3
  • 31
  • 62

1 Answers1

2

I've been working on a plugin for Mason to deal with encoding. $result->output is the wrong place to encode output, because visit will run a subrequest, encoding its own content at the end, before returning to the original component, which then re-encodes everything when it completes. So the content in the visit gets encoded twice. I'm surprised you are having a problem with go, because that discards all previous content and starts again, which should be OK.

Have a look at https://github.com/davebaird/mason-plugin-withencoding

Dave Baird
  • 143
  • 7