Rendering data structures using CGI::Application

2009-06-29 Thread Steve Bertrand
I've been stuck for the last three hours trying to render the following
data structure into my browser. I'm using CGI::Application, and
CGI::Application::Plugin::PageBuilder.

No matter what I do or try, I can not loop the structure into a template
variable. I'm literally at wits end, and am considering just figuring
out a way to 'print' it out.

Any strong kick to the teeth as a boost would be most appreciated. This
is my data structure, and below shows that the particular module that
produces this data can be queried to produce more human readable output:

$error-dump_all();

$VAR1 = \[
'quantity is undefined, zero or illegal',
'amount is undefined or illegal',
'payment is undefined or illegal'
  ];
$VAR2 = \[
{
  'sub' = undef,
  'filename' = 'tests/purchase.pl',
  'line' = 43,
  'package' = 'main'
},
{
  'sub' = 'ISP::Transac::create_transaction',
  'filename' = '../ISP/Transac.pm',
  'line' = 32,
  'package' = 'ISP::Transac'
},
{
  'sub' = 'ISP::Sanity::transaction_data',
  'filename' = '../ISP/Sanity.pm',
  'line' = 55,
  'package' = 'ISP::Sanity'
}
  ];

# I can print it out nicely on the command line, but not in the browser:

my @messages= $error-get_messages();
my @stack   = $error-get_stack();

print join (\n, @messages);

for (@stack) {
while ( my ($key, $value) = each %{$_}) {
print $key = $value\n
if defined $key and
   defined $value;
}
}
}

Is there an easy way to render this to the browser, while keeping HTML
out of my code?

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: Rendering data structures using CGI::Application

2009-06-29 Thread Gurunandan R. Bhat
How do you want the output to look? If you write in a sample output, I
could help with the template to generate that output

Regards


On Mon, 2009-06-29 at 22:56 -0400, Steve Bertrand wrote:

 I've been stuck for the last three hours trying to render the following
 data structure into my browser. I'm using CGI::Application, and
 CGI::Application::Plugin::PageBuilder.
 
 No matter what I do or try, I can not loop the structure into a template
 variable. I'm literally at wits end, and am considering just figuring
 out a way to 'print' it out.
 
 Any strong kick to the teeth as a boost would be most appreciated. This
 is my data structure, and below shows that the particular module that
 produces this data can be queried to produce more human readable output:
 
 $error-dump_all();
 
 $VAR1 = \[
 'quantity is undefined, zero or illegal',
 'amount is undefined or illegal',
 'payment is undefined or illegal'
   ];
 $VAR2 = \[
 {
   'sub' = undef,
   'filename' = 'tests/purchase.pl',
   'line' = 43,
   'package' = 'main'
 },
 {
   'sub' = 'ISP::Transac::create_transaction',
   'filename' = '../ISP/Transac.pm',
   'line' = 32,
   'package' = 'ISP::Transac'
 },
 {
   'sub' = 'ISP::Sanity::transaction_data',
   'filename' = '../ISP/Sanity.pm',
   'line' = 55,
   'package' = 'ISP::Sanity'
 }
   ];
 
 # I can print it out nicely on the command line, but not in the browser:
 
 my @messages= $error-get_messages();
 my @stack   = $error-get_stack();
 
 print join (\n, @messages);
 
 for (@stack) {
 while ( my ($key, $value) = each %{$_}) {
 print $key = $value\n
 if defined $key and
defined $value;
   }
 }
 }
 
 Is there an easy way to render this to the browser, while keeping HTML
 out of my code?
 
 Steve


Re: Rendering data structures using CGI::Application

2009-06-29 Thread Steve Bertrand
Gurunandan R. Bhat wrote:
 How do you want the output to look? If you write in a sample output, I
 could help with the template to generate that output

I would be extremely delighted to even get the following rendered for
now, given the Data::Dumper output below. If I had a decent example,
then I'd be able to learn from it, and go from there:

--- output displayed as I'd see it in the browser---

Error messages:

quantity is undefined, zero or illegal
amount is undefined or illegal
payment is undefined or illegal

Stack trace:

sub = undef;
filename = tests/purchase.pl
line = 43
package = main

sub = ISP::Transac::create_transaction
filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm
line = 32
package = ISP::Transac

sub = ISP::Sanity::transaction_data
filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm
line = 55
package = ISP::Sanity

--- end browser output ---

Here again is the actual data. $VAR1 is an array of the error messages,
and $VAR2 is an array, where each element is a hash which represents a
level in a stack trace.

$VAR1 = \[
'quantity is undefined, zero or illegal',
'amount is undefined or illegal',
'payment is undefined or illegal'
  ];
$VAR2 = \[
{
  'sub' = undef,
  'filename' = 'tests/purchase.pl',
  'line' = 43,
  'package' = 'main'
},
{
  'sub' = 'ISP::Transac::create_transaction',
  'filename' =
'/usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm',
  'line' = 32,
  'package' = 'ISP::Transac'
},
{
  'sub' = 'ISP::Sanity::transaction_data',
  'filename' =
'/usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm',
  'line' = 55,
  'package' = 'ISP::Sanity'
}
  ];

Thank you!

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: Rendering data structures using CGI::Application

2009-06-29 Thread Gurunandan R. Bhat
Sure. I think you would like to have the error messages next to the sub
that threw them, but here is what you want: (NOT TESTED!!)

In your module:

my $messages = [
{text = 'quantity is undefined, zero or illegal'},
{text = 'amount is undefined or illegal'},
{text = 'payment is undefined or illegal'},
];

my $stack = [
{
sub = undef,
filename = 'tests/purchase.pl',
line = 43,
package = 'main',
},
{
sub = 'ISP::Transac::create_transaction',
filename = 
'/usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm',
line = 32,
package = 'ISP::Transac',
},
{
sub = 'ISP::Sanity::transaction_data',
filename = 
'/usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm',
line = 55,
package = 'ISP::Sanity',
},
];

my $tpl = $app-load_tmpl('templatefile.tpl', die_on_bad_params = 0);
$tpl-param(
MESSAGES = $messages,
STACK = $stack,
);

return $tpl-output;

In your template file

ul
!-- TMPL_LOOP NAME=MESSAGES --
li!-- TMPL_VAR NAME=TEXT --/li
!-- /TMPL_LOOP --
/ul

ul
!-- TMPL_LOOP NAME=STACK --
li
ul
!-- TMPL_VAR NAME=sub --br /
!-- TMPL_VAR NAME=filename --br /
!-- TMPL_VAR NAME=line --br /
!-- TMPL_VAR NAME=package --
/ul
/li
!-- /TMPL_LOOP --



HTH


On Mon, 2009-06-29 at 23:51 -0400, Steve Bertrand wrote:
 Gurunandan R. Bhat wrote:
  How do you want the output to look? If you write in a sample output, I
  could help with the template to generate that output
 
 I would be extremely delighted to even get the following rendered for
 now, given the Data::Dumper output below. If I had a decent example,
 then I'd be able to learn from it, and go from there:
 
 --- output displayed as I'd see it in the browser---
 
 Error messages:
 
 quantity is undefined, zero or illegal
 amount is undefined or illegal
 payment is undefined or illegal
 
 Stack trace:
 
 sub = undef;
 filename = tests/purchase.pl
 line = 43
 package = main
 
 sub = ISP::Transac::create_transaction
 filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm
 line = 32
 package = ISP::Transac
 
 sub = ISP::Sanity::transaction_data
 filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm
 line = 55
 package = ISP::Sanity
 
 --- end browser output ---
 
 Here again is the actual data. $VAR1 is an array of the error messages,
 and $VAR2 is an array, where each element is a hash which represents a
 level in a stack trace.
 
 $VAR1 = \[
 'quantity is undefined, zero or illegal',
 'amount is undefined or illegal',
 'payment is undefined or illegal'
   ];
 $VAR2 = \[
 {
   'sub' = undef,
   'filename' = 'tests/purchase.pl',
   'line' = 43,
   'package' = 'main'
 },
 {
   'sub' = 'ISP::Transac::create_transaction',
   'filename' =
 '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm',
   'line' = 32,
   'package' = 'ISP::Transac'
 },
 {
   'sub' = 'ISP::Sanity::transaction_data',
   'filename' =
 '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm',
   'line' = 55,
   'package' = 'ISP::Sanity'
 }
   ];
 
 Thank you!
 
 Steve


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Rendering data structures using CGI::Application

2009-06-29 Thread Steve Bertrand
Gurunandan R. Bhat wrote:
 Sure. I think you would like to have the error messages next to the sub
 that threw them, but here is what you want: (NOT TESTED!!)

..woot!!!

Thank you ever so much.

Although I inserted the data statically into my _render_error() method,
the result is positive!

Other than site-specific changes, your code was completely copy and
paste. Kudos.

Now that I have a baseline to work with, it will be trivial for me to
follow this example to insert my data dynamically, and format it as I
please.

Cheers,

Steve

ps. I've stuck with my 'passing the $error around' for now. I've been
finding that going over older code (a couple of years old) and being
able to rewrite it at 20% its original size is a good thing. Even if my
error checking is the hard way, it's still 'a' way, and one I'll
eventually learn a lesson from ;)


smime.p7s
Description: S/MIME Cryptographic Signature