CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
storing all param for later use

 



hwnd
User

Mar 18, 2013, 8:27 AM

Post #1 of 9 (745 views)
storing all param for later use Can't Post

I was just curious after looking throughout the forums and google, what is the best way to parse all param into a array of hashes or so for later use.

In reference to an example:


Code
  

my %blah;
my @fields = qw(newsdate newshead news); #form field names i want to use, (same name as web form and database column name)

foreach (@fields) {
%blah{$_} = $q->param($_);
}

And then wondering the best way to push into an array or map for using in and INSERT statement, and then using in reference for later use. Or would this be something that is better to do as a self object->



FishMonger
Veteran / Moderator

Mar 18, 2013, 8:35 AM

Post #2 of 9 (743 views)
Re: [hwnd] storing all param for later use [In reply to] Can't Post


Quote

Code
foreach (@fields) {  
%blah{$_} = $q->param($_);
}



That would be better written as:

Code
my %blah = $q->Vars;


Your question is not vary clear. Could you restate your question in clear terms and include the code that is giving you trouble.


hwnd
User

Mar 18, 2013, 5:30 PM

Post #3 of 9 (725 views)
Re: [FishMonger] storing all param for later use [In reply to] Can't Post

I suppose at first I was asking after referencing this through perldoc and google, what is the most effective way to store all params into a array of hashes or hash for later use. Here is my example of what I'm trying to figure out.


Code
   

# Define query
my $q = CGI->new;

# These field names are the form fields name
# and database column names I want to use
my @fields = (
{ what => 'newsdate' },
{ what => 'newshead' },
{ what => 'news' },
);

# Wanting to map the values of each param for later use
# in SELECT or INSERT statments

my @params = map { ($q->param($_->{what})) } @fields;

# Example: qq(INSERT INTO table (Col1, Col2, Col3) VALUES (join(', ', ('?') x @fields)));

# Package file..

sub new {
my $class = shift;
my $self = {
_date => $q->param('newsdate'),
_head => $q->param('newshead'),
_news => $q->param('news'),
};

# ^ instead of using $q->param('') for each, can i
# use from earlier reference such as $_->{what} ?

bless $self, ref( $class ) || $class;
return $self;
}



(This post was edited by hwnd on Mar 19, 2013, 8:14 AM)


FishMonger
Veteran / Moderator

Mar 19, 2013, 9:42 AM

Post #4 of 9 (702 views)
Re: [hwnd] storing all param for later use [In reply to] Can't Post

You still haven't made it clear as to your real goal? What are you really needing to accomplish.

Based on the limited info you given, building an array of hash refs is not needed or wanted and would be very weird.

You could create a simple hash like I previously have showen and pass that by reference to the object constructor or to some other method that adds it to the object.


hwnd
User

Mar 19, 2013, 10:52 PM

Post #5 of 9 (683 views)
Re: [FishMonger] storing all param for later use [In reply to] Can't Post

My real goal is trying to use object, classes, methods to insert, remove, ... with SQL query. At first I was asking about gathering all params into a hash to store for later use which I did simplify how easy that was. Now i'm having trouble working with this class, method. It's not running through the process.


Code
   

my $dbh = hwnd->new('DBI info', '******', '******') or die $DBI::errstr;

$dbh->insert("
tablename", ['col1', 'col2', 'col3'],

['2013-03-20', '
Test', 'blah']

);

# stuff in package module


sub new {

my $class = shift;

my $self = {};

bless $self, ref( $class ) || $class;

$self->{dbh} = DBI->connect( @_ );

return $self;

}



sub insert {

my $self = shift;

my($tbl, $col, $val) = @_;

my $new_tbl = $self->{dbh}->quote($tbl);

my @new_col = map { $self->{dbh}->quote($_) } @$col;

my @placers = map { "?" } @new_col;

my $query = qq(

INSERT INTO $new_tbl

( @{[ join(', ', @new_col) ]} )

VALUES ( @{[ join(', ', @placers ]} )

);

return $self->{dbh}->do($query, undef, @$val);

}



(This post was edited by hwnd on Mar 19, 2013, 11:14 PM)


FishMonger
Veteran / Moderator

Mar 20, 2013, 7:44 AM

Post #6 of 9 (672 views)
Re: [hwnd] storing all param for later use [In reply to] Can't Post

What do you mean by "It's not running through the process."?

At what point is it failing?

What errors/warnings are you receiving?


Quote

Code
my $dbh = hwnd->new('DBI info', '******', '******') or die $DBI::errstr;


That's the wrong place to put the die statement. It should be on the connect statement in your constructor.


hwnd
User

Mar 20, 2013, 8:04 PM

Post #7 of 9 (651 views)
Re: [FishMonger] storing all param for later use [In reply to] Can't Post

Ok here's where its confusing the process. Is there a way to simplify this?


Code
 
# Code i'm using to pass it to the object

$dbh->insert("tablename", ['col1', 'col2', 'col3'], ['1', '2', '3']);

# It returns one placeholder (?) doing it like this:

my @new_col = (join(', ', map { $_ } @$col));
my @placers = (join(',', map { '?' } @new_col));

my $query = qq( INSERT into $tbl (@new_col) VALUES (@placers) );

# It returns the correct number of placeholders like this:

my @new_col = map { $_ } @$col;
my @placers = map { '?' } @new_col;

my $query = qq( INSERT into $tbl (@{[join(', ', @new_col)]}) VALUES (@{[ join(',', @placers)]}) );


FishMonger
Veteran / Moderator

Mar 22, 2013, 7:01 AM

Post #8 of 9 (642 views)
Re: [hwnd] storing all param for later use [In reply to] Can't Post

Is this what you're after?

Code
my $tables = join ',', @$col; 
my $places = join ',', ('?') x @$col;
my $query = qq(INSERT into $tbl ($tables) VALUES ($places) );



hwnd
User

Mar 22, 2013, 5:22 PM

Post #9 of 9 (632 views)
Re: [FishMonger] storing all param for later use [In reply to] Can't Post

It is. I've decided to use hash key/values instead, something like:


Code
  

$obj->insert( { table => 'foo' }, { col1 => 'foo', col2 => 'bar' } );

my ($self, $tbl, $rec) = @_;

my @fields = keys %$rec;
my @values = map { $rec->{$_} } @fields;
my @placeh = map { '?' } @fields;

local $" = ',';

my $query = qq( INSERT INTO $tbl->{table} (@fields) VALUES(@placeh) );


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives