What is the recommended way how to prepend some text at the beginning of an
object file?

I wanted all my templates to be considered utf8 source files. But I didn't
want to write

<%class>
use utf8;
....
</%class>

in each template.

So I created custom Interp class and
overwrote Mason::Interp::write_object_file

around write_object_file => sub {
    my $orig = shift;
    my ( $this, $object_file, $object_contents, @rest ) = @_;

    $object_contents = "use utf8;\n$object_contents";
    return $orig->( $this, $object_file, $object_contents, @rest );
};

But I found very quickly that this little modification broke my
inheritance. The reason was that generated code relies on # FLAGS comment
to be at the very first line of the object file:

# FLAGS: {"extends":"/Page.mc"}
our ($_class_cmeta, $m, $_m_buffer, $_interp);
BEGIN {
local $_interp = Mason::Interp->current_load_interp;
$_interp->component_moose_class->import;
$_interp->component_import_class->import;
}
...


I modified the custom class and put use utf8 after FLAGS line if present:

around write_object_file => sub {
    my $orig = shift;
    my ( $this, $object_file, $object_contents, @rest ) = @_;

    # all content files are utf8
    # 2012-01-22 we must respect the flags
    $object_contents =~ /^(?:#\s*FLAGS.*?\n)?/sg;
    $object_contents =~ s/\G/use utf8;\n/;

    return $orig->( $this, $object_file, $object_contents, @rest );
};

It works but I am not sure how reliable this method is. Aren't there any
other lines except of FLAGS which further processing relied upon?

Thanks for any reply

Roman
------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to