EzDevInfo.com

Mason

Mason.js for creating a perfect grid with jQuery. Mason by DrewDahlman

Why am I getting a panic with Perl 5.10, Mason, and Apache?

I'm developing an application using Perl 5.10, HTML::Mason, and apache 2.2. This is the first time I've used Perl 5.10 for a big project. I get a strange behavior every once in a while. The application dies with a very strange error:

panic: attempt to copy value
to a freed scalar b87acf34 at ...

I guess my question is it Perl 5.10 because I've never experienced this before, or is it some other faulty dependency?

Thanks a lot!


Source: (StackOverflow)

Defining global variables in perl Mason Framework

I recently came across a problem: how to define a global variable in perl Mason Framework ? I need to define a variable and assign a value to it which will be visible from all Mason components, but all my attempts happen to be unsuccessful, because components tell that they don't see the variable.

Thanks in advance.


Source: (StackOverflow)

Advertisements

How to make Mason2 UTF-8 clean?

Reformulating the question, because

Comment: This question already earned the "popular question badge", so probably i'm not the only hopeless person. :)

Unfortunately, demonstrating the full problem stack leads to an very long question and it is very Mason specific.

First, the opinions-only part :)

I'm using HTML::Mason over ages, and now trying to use Mason2. The Poet and Mason are the most advanced frameworks in the CPAN. Found nothing comparamble, what out-of-box allows write so clean /but very hackable :)/ web-apps, with many batteries included (logging, cacheing, config-management, native PGSI based, etc...)

Unfortunately, the author doesn't care about the rest of the word, e.g. by default, it is only ascii based, without any manual, faq or advices about: how to use it with unicode

Now the facts. Demo. Create an poet app:

poet new my #the "my" directory is the $poet_root
mkdir -p my/comps/xls
cd my/comps/xls

and add into the dhandler.mc the following (what will demostrating the two basic problems)

<%class>
    has 'dwl';
    use Excel::Writer::XLSX;
</%class>
<%init>
    my $file = $m->path_info;

    $file =~ s/[^\w\.]//g;
    my $cell = lc join ' ', "ÅNGSTRÖM", "in the", $file;

    if( $.dwl ) {
        #create xlsx in the memory
        my $excel;
        open my $fh, '>', \$excel or die "Failed open scalar: $!";
        my $workbook  = Excel::Writer::XLSX->new( $excel );
        my $worksheet = $workbook->add_worksheet();
        $worksheet->write(0, 0, $cell);
        $workbook->close();

        #poet/mason output
        $m->clear_buffer;
        $m->res->content_type("application/vnd.ms-excel");
        $m->print($excel);
        $m->abort();
    }
</%init>
<table border=1>
<tr><td><% $cell %></td></tr>
</table>
<a rel='nofollow' href="?dwl=yes">download <% $file %></a>

and run the app

../bin/run.pl

go to http://0:5000/xls/hello.xlsx and you will get:

+----------------------------+
| ÅngstrÖm in the hello.xlsx |
+----------------------------+
download hello.xlsx

Clicking the download hello.xlsx, you will get hello.xlsx in the downloads.

The above demostrating the first problem, e.g. the component's source arent "under" the use utf8;, so the lc doesn't understand characters.

The second problem is the following, try the [http://0:5000/xls/hélló.xlsx] , or http://0:5000/xls/h%C3%A9ll%C3%B3.xlsx and you will see:

+--------------------------+
| ÅngstrÖm in the hll.xlsx |
+--------------------------+
download hll.xlsx
#note the wrong filename

Of course, the input (the path_info) isn't decoded, the script works with the utf8 encoded octets and not with perl characters.

So, telling perl - "the source is in utf8", by adding the use utf8; into the <%class%>, results

+--------------------------+
| �ngstr�m in the hll.xlsx |
+--------------------------+
download hll.xlsx

adding use feature 'unicode_strings' (or use 5.014;) even worse:

+----------------------------+
| �ngstr�m in the h�ll�.xlsx |
+----------------------------+
download h�ll�.xlsx

Of course, the source now contains wide characters, it needs Encode::encode_utf8 at the output.

One could try use an filter such:

<%filter uencode><% Encode::encode_utf8($yield->()) %></%filter>

and filter the whole output:

% $.uencode {{
<table border=1>
<tr><td><% $cell %></td></tr>
</table>
<a rel='nofollow' href="?dwl=yes">download <% $file %></a>
% }}

but this helps only partially, because need care about the encoding in the <%init%> or <%perl%> blocks. Encoding/decoding inside of the perl code at many places, (read: not at the borders) leads to an spagethy code.

The encoding/decoding should be clearly done somewhere at the Poet/Mason borders - of course, the Plack operates on the byte level.


Partial solution.

Happyly, the Poet cleverly allows modify it's (and Mason's) parts, so, in the $poet_root/lib/My/Mason you could modify the Compilation.pm to:

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

what will insert the wanted preamble into every Mason component. (Don't forget touch every component, or simply remove the compiled objects from the $poet_root/data/obj).

Also you could try handle the request/responses at the borders, by editing the $poet_root/lib/My/Mason/Request.pm to:

#found this code somewhere on the net
use Encode;
override 'run' => sub {
    my($self, $path, $args) = @_;

    #decode values - but still missing the "keys" decode
    foreach my $k (keys %$args) {
        $args->set($k, decode_utf8($args->get($k)));
    }

    my $result = super();

    #encode the output - BUT THIS BREAKS the inline XLS
    $result->output( encode_utf8($result->output()) );
    return $result;
};

Encode everything is an wrong strategy, it breaks e.g. the XLS.

So, 4 years after (i asked the original question in 2011) still don't know :( how to use correctly the unicode in the Mason2 applications and still doesn't exists any documentation or helpers about it. :(

The main questions are: - where (what methods should be modified by Moose's method modifiers) and how correctly decode the inputs and where the output (in the Poet/Mason app.)

  • but only textual ones, e.g. text/plain or text/html and such...
  • a do the above "surprise free" - e.g. what will simply works. ;)

Could someone please help with real code - what i should modify in the above?


Source: (StackOverflow)

Why would Perl's CGI::cookie be able to set a cookie in IE, but not Firefox?

I have a bit of Perl CGI code which I'm trying to run in the project web space of a SourceForge account. The code is able to set a browser cookie just fine when talking to IE, but the cookie is not set when talking to Firefox. When I test with Apache on "localhost", both browsers work fine. It's only on the remote SourceForge URL that Firefox craps out.

A search has turned up dozens of near-duplicate questions, but usually people have the exact opposite problem! (Firefox being fine and IE having the problem)

Here is the utility sub I'm calling to set cookies:

sub setCookie {
    my $name = shift;
    my $value = shift;
    my $expires = shift;
    my $path = shift;
    my $domain = shift;
    if( !defined( $expires ) ) {
        $expires = '+4h';
    }
    if( !defined( $path ) ) {
        $path = '/';
    }
    if( !defined( $domain ) ) {
        $domain = 'steveperkins.sourceforge.net';
    }
    my $cookie = CGI::cookie(
        -name    => $name,
        -value   => $value,
        -domain   => $domain,
        -expies => $expires,
        -path    => $path
    );
    $r->header_out('Set-cookie' => $cookie);
}

Any ideas? My first thought was some kind of subdomain issue, because my SourceForge project URL has a subdomain in it while "localhost" does not. I've experimented with setting the cookie domain to my specific subdomain, or to just the base "sourceforge.net". It doesn't seem to make a difference either way.

UPDATE: Someone in the comments below asked about the HTTP response headers. I've used the network traffic analyzer tool Wireshark to monitor the request and response headers for both IE and Firefox, and here's what they look like:

IE (works)

Request

GET http://myproject.sourceforge.net/cgi-bin/myscript.cgi?page=user&userID=1 HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://myproject.sourceforge.net/cgi-bin/myscript.cgi
Accept-Language: en-us
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.5.30729; InfoPath.1; .NET CLR 3.0.30618)
Proxy-Connection: Keep-Alive
Host: myproject.sourceforge.net
Authorization: Basic [password omitted]

Response

HTTP/1.1 200 OK
Server: nginx/0.7.63
Date: Tue, 26 Oct 2010 18:23:49 GMT
Content-Type: text/html; charset=ISO-8859-1
Expires: Thu, 28 Oct 2010 18:23:49 GMT
Cache-Control: max-age=172800, proxy-revalidate
Transfer-Encoding: chunked
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Content-Encoding: gzip
Set-Cookie: USER=1; domain=myproject.sourceforge.net; path=/

Firefox (doesn't work)

Request

GET http://myproject.sourceforge.net/cgi-bin/myscript.cgi HTTP/1.1
Host: myproject.sourceforge.net
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Proxy-Connection: keep-alive
Cookie: __utma=191645736.1501259260.1287701281.1288028150.1288100562.10; __utmz=191645736.1288101011.10.10.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=sourceforge%20project%20web%20space%20basic%20auth; _jsuid=4215309712123065236
Authorization: Basic [password omitted]

Response

HTTP/1.1 200 OK
Server: nginx/0.7.63
Date: Tue, 26 Oct 2010 18:17:58 GMT
Content-Type: text/html; charset=ISO-8859-1
Expires: Thu, 28 Oct 2010 18:17:58 GMT
Cache-Control: max-age=172800, proxy-revalidate
Transfer-Encoding: chunked
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Content-Encoding: gzip
Age: 0

Source: (StackOverflow)

Perl: show used subroutines

In some project ( Mason ) there is a lot of perl modules (> 200). Really used only 5-10% from this code. How i can look, which subroutines is used (or unused)?


Source: (StackOverflow)

Good IDE or syntax highlighting editor for Mason?

Can anyone recommend some good IDEs or editors for Mason?

At this point I'm just using VIM but it would be nice to have an editor that supports syntax highlighting as well as general syntax checking.

There are plenty that support Perl of course, but when it comes to files with Mason and Perl intertwined the editors tend to get confused and highlight incorrectly.


Source: (StackOverflow)

.mpl extension in URL : Perl related technology?

I've seen a company using this kind of URLs http://www.foob.ar/cgi-perl/bar.mpl. It seems they use Perl in the back-end, but I would like to know which technology (framework, modules, etc.) is supposed to be involved by the .mpl extension ?

I believe it would be Mason template engine but can't find a confirmation.


Source: (StackOverflow)

How can I extract <%doc> content from Mason?

Is there a tool like javadoc to extract the <%doc> content and the various methods with their parameters in Mason (Perl)? I'd like to add some basic documentation to components I'm writing and it would nice for the documentation to be auto-generated from the code.

Edited to answer question from benrifkah:

My intention was to look for a tool that functions like javadoc. It uses the comments in <%doc> and also extracts the info from <%method> and <%def> blocks.


Source: (StackOverflow)

Is Mason a framework?

I've been having an agruement with a friend that Mason (Perl) is not a framework, but a templating language. I feel Mason for Perl does what JSP does for Java (as an analogy, not pure technical comparison). From what I know, JSP is not a framework and I feel neither is Mason. When I looked up wikipedia Mason (Perl), I see that the main site says it is a web application framework written in Perl while the discussion page contests it.

Any pointers on why it is/ it is not a framework?

Update based on comments from ysth: For a framework, I feel it should at least make db access easy, manage sessions, basic security that a webapp would need, templating and code reuse (or libraries that make basic tasks easy).


Source: (StackOverflow)

Perl: Javascript::V8 templates - from the perl

Looking for template engine like HTML::Mason (or Mason), so what "compiles" source components into perl code, but instead of perl-code will "compile" components into JavaScript code and after run/execute them with Javascript::V8 perl module.

Motivation: Looking for solution for safe template language, what can edit users without compromising the server security. JavaScript is full featured language so using it is probably better/faster than some "mini languages" like TT or similar. The best for me would be an extension (rewrite) of Mason for compiling into Joose/JavaScript instead of Moose/Perl. ;)

And yes, want do this from perl with Javascript::V8 because this way is possible having all perl's power available via Javascript::V8 $context->bind_function in very safe way.

Questions:

  • Anyone know something like? (found nothing in CPAN)...

EDIT: in Mason you can write for example

% #perl version
% my(@list) = qw(Jane John Doe);
<ul> 
% foreach my $item (@list) { 
  <li><% uc($item) %></li> 
% } 
</ul>

would be nice to have possibility write the above in JS, like:

% //javascript version
% var list = ["Jane", "John", "Doe"];
<ul> 
% for(var i in list) {
  <li><% perl_uc($list[i]) %></li>
  <!-- the "perl_uc" is the real perl uc() what is binded
       with Javascript::V8::bind_function(perl_uc => sub { return uc(@_) }
  -->
% } 
</ul>

The above source should be "compiled" into JavaScript (Joose), and executed with Javascript::V8. (like in Mason - the source is compiled into perl/Moose object and executed with perl)...

As you can see, the for(var i in list) is written in pure JS, and not in "mini-language"…


Source: (StackOverflow)

How to "use" another module in perl mason?

In a regular perl script you can write

use Foo::Crypt::Employee qw(foo);

at the top of your file to use the foo subroutine throughout the rest of your script.

How can I do this in a perl mason file? Where would I put such a line of code?


Source: (StackOverflow)

Global Variable mason2 in POET

I'm new to Mason2/POET and I have been using this guide http://search.cpan.org/~jswartz/Poet/lib/Poet/Manual/Tutorial.pod to create my first website.

Now I would like to create a new global variable (example: $User) but then I have no idea or what direction I should take in order to do so since the document doesnt explain about it. Most documents I found were about Apache or mod_perl...

Example of what I'm looking for:

<%augment wrap>
 <html>
  html code goes here
 </html>
</%augment>
<%init>
my $User;
Mason::Interp::allow_globals => [qw($User)];
</%init>

Source: (StackOverflow)

Where can I find a Perl module for converting a Perl data structure into a JavaScript one?

Where can I find a Perl module for converting a Perl data structure into a JavaScript one?

e.g. this is my code (Mason):

% # convert our @cti data structure into a javascript one
  var cti = [
% foreach my $cti_category (@cti) {
             {
                 label: "<% $cti_category->{'label'} %>",
                 value: "<% $cti_category->{'value'} %>",
                 children: [
%     foreach my $cti_type (@{$cti_category->{'children'}}) {
                            {
                              label: "<% $cti_type->{'label'} %>",
                              value: "<% $cti_type->{'value'} %>",
                            },
%     }
                           ]
             },
% }
            ];

is there a module for this?


Source: (StackOverflow)

Perl Mason Syntax Validation

Is there a way to validate perl mason syntax at the command line? I know for regular perl modules you can just use perl -c, but that throws errors for mason-specific syntax like docstrings and the like...

For example:

<%doc>
DOCUMENTATION SHOULD NOT GET PARSED
</%doc>

<%args>
$args
</%args>

<%perl>
my $var = $args->{var};
</%perl>

is a valid perl mason file, but running perl -c against it returns:

Semicolon seems to be missing at path/to/file.mc line 1.
syntax error at path/to/file.mc line 2, near "DOCUMENTATION S"
path/to/file.mc had compilation errors.

Source: (StackOverflow)

Mason methods scope - override

Using Mason2. Have 3 components.

/Base.mc
/tmp/Base.mc
/tmp/index.mc

The /tmp/index.mc with a content:

hello from <% $m->request_path %></br>
<% $.Some %>

the $.Some is an method defined in the /Base.mc:

<%augment wrap><% inner() %></%augment>

<%method Some>
The default "Some" method defined in the <% __PACKAGE__ %>
</%method>

the /tmp/Base.mc contains only

<%augment wrap><% inner() %></%augment>

Requesting /tmp/index prints:

hello from /tmp/index
The default "Some" method defined in the MC0::Base_mc

Now added the Some method into /tmp/Base.mc

<%method Some>
Redefined "Some" method in <% __PACKAGE__ %>
</%method>

Requesting /tmp/index again, it prints:

hello from /tmp/index
Redefined "Some" method in MC0::tmp_Base_mc

It honored the redefined Some method in the wrapped /tmp/Base.mc

The question is:

If Mason allows redefine methods as above, what is the purpose of the <%override method>? Does the <%override Some> something differently? (when i tested, it prints the same).

EDIT Maybe the question can be reduced to the following perl code.

use 5.014;
use warnings;

package My;
use Moose;
sub some { say "some from " . __PACKAGE__ }

package My2;
use Moose;
extends 'My';
sub some { say "another some from " . __PACKAGE__ }
#the above line is an correct way to refefine a sub "some"?
#so don;t need to use the
#override 'some' => sub { say "another some from " . __PACKAGE__ };

package main;
use My2;
my $m = My2->new();
$m->some();

in both cases (e.g. "plain" redefine and redefine with "override") prints:

another some from My2

So, the only difference is the possibility of calling the super() in the some with override? and sorry if I missed some basic knowlegde... ;(


Source: (StackOverflow)