Gossamer Forum
Home : General : Perl Programming :

Simple but cool!

Quote Reply
Simple but cool!
Hehe, I was writing some code and got lazy and thought I'd try playing about.

Did anyone know you could do stuff like:

Code:
%{ Class->method }

I was so excited when it worked :)

Im using:
Code:
$obj->parse($template, { error => $error, %{ Base->get_hash } } );

Cool

get_hash returns a hashref of form input via get/post

Last edited by:

Paul: Apr 8, 2002, 10:28 AM
Quote Reply
Re: [Paul] Simple but cool! In reply to
You mean like:

%$hashref

? It's the same thing. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Simple but cool! In reply to
Yeah, I just didn't realise you could do it that way...I was sure it would spew an error :)

I'd been doing it like:

my $input = Base->get_hash;

..then using:

{ %$input }

....but in my laziness I just did

%{ Base->get_hash }

and was shocked/pleased it worked Laugh

Last edited by:

Paul: Apr 8, 2002, 11:27 AM
Quote Reply
Re: [Alex] Simple but cool! In reply to
Hm. I think I've been doing something similar for a while to load up the hash %tmpl with name/value pairs returned from a database qeury:

Code:
my $sqlquery = qq|SELECT * FROM ata_members|;
my $sth = $dbh->prepare($sqlquery);
$sth->execute or &error_html("Can't execute SQL query:[$DBI::err] $DBI::errstr");
my $ref = $sth->fetchrow_hashref();
$sth->finish;

%tmpl = %$ref;

It basically loads up %tmpl with column name and column value for each row returned. Preety funky I thought.

- wil
Quote Reply
Re: [Wil] Simple but cool! In reply to
Hmm its simliar but basically you are just copying a hash.

Its the same as

$foo = $bar

or

$bar = \'foo';

$foo = $$bar

Last edited by:

Paul: Apr 9, 2002, 1:24 AM
Quote Reply
Re: [Paul] Simple but cool! In reply to
Yeah, I guess so. But it's really cool for getting values out of my database quickly and into my template hash for later use.

- wil
Quote Reply
Re: [Wil] Simple but cool! In reply to
Or it would be quicker if your parser accepted a hashref :)

Then if you were lazy like me you could do:

$obj->your_parser($template, $sth->fetchrow_hashref );
Quote Reply
Re: [Paul] Simple but cool! In reply to
My parser can barely accept a hash! Blush

I need to seriously start looking at some CPAN modules for Template parsing. The wheel has been re-invented many times already.

http://www.perl.com/...8/21/templating.html

- wil
Quote Reply
Re: [Wil] Simple but cool! In reply to
If no-one reinvented the wheel then "choice" wouldn't exist in the dictionary Angelic