First off, I would suggest reading Perl Cookbook and Perl Best Practices. Running perlcritic on your code is never a bad idea and will catch many 'newbie' errors.

Second, test your code in isolation of the Mason site to make sure it's working as intended.

Third, put debugging messages in your code to sanity-check yourself. Two good ways of doing this are to use CGI::Carp (if you want the error messages to go to the webserver error log) or print debugging statements as HTML comments from inside your mason components:
EG:
<!-- <% $debug_message %> -->

It may be a matter of style, but in Mason I prefer to factor out any big chunks of raw perl code as functions and put them in a module, which I then use in the Mason components. I try to avoid having any business logic in Mason components if I can. I find this makes it easier to develop and test the business logic in isolation of the site. YMMV, but I prefer to write a module with functions that return data structures (hashs, arrays, HoH, etc), and then have the mason components transform those structures into html (or xml, or json, or whatever).

Cleaning up your code:
<%perl>
use CGI::Carp;
my $filename = '/path/to/archivio';
open FILE, '<', $filename
    or croak "Can't open $filename";
</%init>

<!-- html-ize contents of file -->
<h2>Contents of <% $filename %>:</h2>
<pre>
% while ( my $p = <FILE> ) {
<% $p %>
% }
</pre>


I prefer to use:

<% $p %>

versus  using:

% print $p;

because the <% %> construct is generally safer and allows you to use filters. EG, if the file has valid html, you probably want to turn off the automatic html escaping behavior: <% $p | n %>

Note that this code is HIGHLY unsafe, at least potentially. If the contents of $filename come from an untrusted source or contain unvalidated user-provided data, you're setting yourself up for a whole class of attacks. Best practice is to always validate your input, even if it comes from a "trusted" source.

If you absolutely must use a relative path, try putting this in your mason component and view source on the output in your browser:
% use Cwd;
<!-- Current directory: <% getcwd() %> -->

I'll wager that the result isn't what you expect it to be. It's almost definitely *NOT* going to be the same directory as the current component, which is what you . For that you probably want:
% my $cwd =  $m->interp->comp_root . $m->current_comp->dir_path

then you can do:

% open FILE, '<', "$cwd/archivio" or croak "Can't open $cwd/archivio";

On 2/28/2012 7:05 AM, Eugenio Duran Aroche wrote:

I need know how open, write and read files in Mason, I put this code and not show nothing in the browser, neither can't write in the file
<%perl>

open(FILE,"<archivo");
my @paquetes = <FILE>;
close(FILE);

foreach my $i (@paquetes){
    print $i;
    }
</%perl>

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to