Want cookbook for HTML::Mason -> Mason2

View: New views
2 Messages — Rating Filter:   Alert me  

Want cookbook for HTML::Mason -> Mason2

by Eddie Rowe :: Rate this Message:

| View Threaded | Show Only this Message

The memory leaks in HTML::Mason (1.35) have finally annoyed my ISP to the point that I had to promise them I would upgrade to Mason2.  That said, I'm really having a hard time juggling all the moving parts that seem to be documented as having been changed in the new version.  

I looked through most of the archive and couldn't find what I needed.

I'm hoping that in getting my own questions answered we can all then have a suitable migration document.  I will outline the individual pieces that construct my Mason 1.35 website and then ask what about each piece needs to change.

/* httpd.conf */
PerlModule /path/to/HTML::Mason::ApacheHandler
PerlSetVar MasonDataDir /var/www/mason
PerlSetVar MasonCompRoot /var/www/html
<LocationMatch "/.*\.html$">
    SetHandler perl-script
    PerlHandler HTML::Mason::ApacheHandler
</LocationMatch>

Is there a one-to-one translation in httpd.conf for the above directives?


In the autohandler, I use HTML::Mason to create sessions as follows.
/* autohandler */
<%init>
{ package HTML::Mason::Commands;
   use vars qw(%session);
   use CGI::Cookie;
   use Apache::DBI;
   use Apache::Session::DBI;
   use Apache::Session::MySQL;
}

# Get the incoming cookies
my %cookies = parse CGI::Cookie($r->headers_in->{Cookie});

# Try to re-establish an existing session
eval {
    tie %HTML::Mason::Commands::session, 'Apache::Session::MySQL',
    ($cookies{$cookie_name} ? $cookies{$cookie_name}->value() : undef),
    {
         Handle => $dbh,
         LockHandle => $dbh
    }
    ;
};
...
</%init>

Is the <%init> block still used the same way?  Could I just change HTML::Mason to Mason2.12 and everything work wrt sessions?

/* general component calls */
$m -> call_next(dbh => $dbh)
I saw a note to use Moose inner, but without elaboration.  How do I translate this?

$m -> comp('/path/to/component.mas:methodname');
Does this change at all in Mason 2?

<& /path/to/component.mas, dbh=>$dbh &>
Does this change at all in Mason 2?

Just to confirm, $ARGS{name} is now $.args->{name} ?


"<%args> and <%shared> are gone. Use Moose attributes instead."  What does this mean practically?  What would
<%args>
 $dbh
</%args>
look like in Moose?

Thanks for everything. I have loved using Mason to build my site which is for a animal rescue organization.  The dogs and I thank you!

Eddie Rowe

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mason-users mailing list
Mason-users@...
https://lists.sourceforge.net/lists/listinfo/mason-users

Re: Want cookbook for HTML::Mason -> Mason2

by Jonathan Swartz :: Rate this Message:

| View Threaded | Show Only this Message

On May 17, 2012, at 8:28 AM, Eddie Rowe wrote:

The memory leaks in HTML::Mason (1.35) have finally annoyed my ISP to the point that I had to promise them I would upgrade to Mason2.  That said, I'm really having a hard time juggling all the moving parts that seem to be documented as having been changed in the new version.  

Wasn't aware of the memory leaks - did you try updating to the latest HTML::Mason 1.49? That said, I'm happy to offer guidance for the upgrade. :) I think you'll be happy in the long run.

I'd highly recommend using Poet+Mason. Poet is a web framework designed specifically as a complement to Mason 2; it has all of the "web stuff" that used to be in Mason. I'll assume you are doing that from now on.

I looked through most of the archive and couldn't find what I needed.

I'm hoping that in getting my own questions answered we can all then have a suitable migration document.  I will outline the individual pieces that construct my Mason 1.35 website and then ask what about each piece needs to change.

/* httpd.conf */
PerlModule /path/to/HTML::Mason::ApacheHandler
PerlSetVar MasonDataDir /var/www/mason
PerlSetVar MasonCompRoot /var/www/html
<LocationMatch "/.*\.html$">
    SetHandler perl-script
    PerlHandler HTML::Mason::ApacheHandler
</LocationMatch>

Is there a one-to-one translation in httpd.conf for the above directives?

First, you'll use the 'poet' command to create your environment:

    % poet new MyApp
    my_app/.poet_root
    my_app/bin/app.psgi
    my_app/bin/get.pl

then move your components into my_app/comps, your scripts into my_app/bin, etc.

For mod_perl you can use this configuration:

   <Location />
     SetHandler perl-script
     PerlResponseHandler Plack::Handler::Apache2
     PerlSetVar psgi_app /path/to/my_app/bin/app.psgi
     DefaultType text/html
   </Location>

Mason configuration (comp_root, data_dir, etc.) will now be automatically determined by poet, but you can put overrides in my_app/lib/MyApp/Mason.pm or in configuration.

In the autohandler, I use HTML::Mason to create sessions as follows.
/* autohandler */

You'll need to rename your autohandler to Base.mc

<%init>
{ package HTML::Mason::Commands;
   use vars qw(%session);
   use CGI::Cookie;
   use Apache::DBI;
   use Apache::Session::DBI;
   use Apache::Session::MySQL;
}

# Get the incoming cookies
my %cookies = parse CGI::Cookie($r->headers_in->{Cookie});

# Try to re-establish an existing session
eval {
    tie %HTML::Mason::Commands::session, 'Apache::Session::MySQL',
    ($cookies{$cookie_name} ? $cookies{$cookie_name}->value() : undef),
    {
         Handle => $dbh,
         LockHandle => $dbh
    }
    ;
};
...
</%init>

Is the <%init> block still used the same way?  

Yes, but since this is an autobase component and you're going to wrap the main component, do this instead:
   
   <%class>
   around 'render' => sub {
       # Put your init code here
   };
   </%class>

This says, "when the render method gets called on the main page component, let me do stuff first, then I'll call inner() to call the main page component".

Could I just change HTML::Mason to Mason2.12 and everything work wrt sessions?

It should work, but I recommend getting rid of all that and just using the built-in session. By default it will be stored in the filesystem, but you can change it to use MySQL, e.g. by putting this in bin/app.psgi with this:

    enable "Plack::Middleware::Session",
      store => Plack::Session::Store::Cache->new(
        cache => CHI->new(driver => 'DBI', dbh => $dbh, create_table => 1 )
      );

Then you just use $m->session to get the session hash:

    $m->session->{foo} = 'bar';


/* general component calls */
$m -> call_next(dbh => $dbh)
I saw a note to use Moose inner, but without elaboration.  How do I translate this?

Just call inner().


$m -> comp('/path/to/component.mas:methodname');
Does this change at all in Mason 2?

$m->construct('/path/to/component.mas')->methodname()

That is, $m->construct('/path/to/component.mas') creates a new instance of the component class. If you were calling that component like normal (as with $m->comp or <& &>), you'd call main() on it. But instead you are calling methodname() on it.


<& /path/to/component.mas, dbh=>$dbh &>
Does this change at all in Mason 2?

Nope, amazingly, that will still work. :)


Just to confirm, $ARGS{name} is now $.args->{name} ?

Yes.

"<%args> and <%shared> are gone. Use Moose attributes instead."  What does this mean practically?  What would
<%args>
 $dbh
</%args>
look like in Moose?

    <%class>
    has $dbh => (required => 1);
    </%class>

And now, you'll have to refer to $.dbh instead of $dbh. This is annoying, I realize as I write this - probably the single biggest labor of migrating. :/


Thanks for everything. I have loved using Mason to build my site which is for a animal rescue organization.  The dogs and I thank you!

Aww! No surer way to get help than to tell me that. :p

Hope this helped. Let me know how things work or don't work as you proceed, and if you have any additional questions.

Jon



------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mason-users mailing list
Mason-users@...
https://lists.sourceforge.net/lists/listinfo/mason-users