Dereference a HASH reference

2012-06-18 Thread Mark Haney
I'm at my wits end.  I've pulled every trick I know (which ain't many) 
and I still can't get this working.  Please, for the love of pete (and 
my sanity) help with this.


I'm building a web page that has a series of checkboxes each with a day 
of the week.  Pretty straightforward, right?  Well, from that page I'm 
passing the params to a CGI script that does this:



my %attrs = (
shift_name = $q-param('shift_name'),
shift_beg = $q-param('shift_beg'),
shift_end = $q-param('shift_end'),
shift_days = $q-param('dow'),
factory_id = $q-param('factory'),
);


From there I pass a reference to the hash to a subroutine 
($dao-insert_shift(\%attrs);


Here's where it blows up.  This is the sub:

sub insert_shift {
  my $self = shift;
  my($attrs) = @_;

  my $m = $self-schema-resultset('Shifts')-new($attrs);
  $m-insert;
}

From here it gets inserted into a DB table (called shifts, obviously).

Here's my problem, with the original %attrs I was getting the 'dow' 
params but the insert into the DB bombs because the param('dow') isn't a 
scalar(?).  I did have someone tell me that I needed the line:


shift_days = $q-param('dow'),

to be:

shift_days = [ $q-param('dow') ],

At that point I get all the data inserted into the database EXCEPT the 
DOW (day of week) parameter, which I only get this: ARRAY(0x9517d38)


This is where I'm stuck.  I know I need to dereference that hash, but 
for the life of me I can't figure it out.


Early on, when I realized that the 'dow' was being passed as an array I 
tried to convert that array into a string, but it never did work. At 
least i couldn't make it work.


Does anyone have some idea on what I should do next?  At this point, 
I'll try anything short of shooting my dog.


Help.


--

Mark Haney
Software Developer/Consultant
AB Emblem
ma...@abemblem.com
Linux marius.homelinux 3.3.8-1.fc16.x86_64 GNU/Linux


Dereference a HASH reference - SOLVED

2012-06-18 Thread Mark Haney
Hmm, maybe talking it out (so to speak) helped.  I found a way of (or 
the proper syntax to) allow me to JOIN the day of week array into a 
string which fixes the problem of the hash.  I'm not sure why the join 
works now and not a week ago, but at this point I don't care.




--

Mark Haney
Software Developer/Consultant
AB Emblem
ma...@abemblem.com
Linux marius.homelinux 3.3.8-1.fc16.x86_64 GNU/Linux



Re: Dereference a HASH reference

2012-06-18 Thread David Nicol
this is entirely off-topic for dbi-users. That said, what you're seeing is
due to $q-param('dow') called in array context returning some number
of things other than one thing. There are various ways to fix it,
depending on how $q works. The approach you tried, putting one of the
param lookups inside an anon arrayref constructor, is halfway there --
you need to reach into that array later when you use it.

The situation is also a subtle security flaw, as a user can in theory
construct funny form data with multiple entries for names and
overwrite
for instance shift_beg by giving three 'factory' data, the middle one
being 'shift_beg. Wrapping one of them in square brackets doesn't
help with the general exploit.

 my %attrs = (
        shift_name = $q-param('shift_name'),
        shift_beg = $q-param('shift_beg'),
        shift_end = $q-param('shift_end'),
        shift_days = $q-param('dow'),
        factory_id = $q-param('factory'),
 );

This:

my %attrs = (
        shift_name =  scalar($q-param('shift_name')),
        shift_beg =  scalar($q-param('shift_beg')),
        shift_end =  scalar($q-param('shift_end')),
        shift_days = scalar( $q-param('dow')),
        factory_id = scalar( $q-param('factory')),
);

will always give you exactly one thing in the value slots, then
depending on what you see there you can adjust.


-- 
[Feynman]'s wife was granted a divorce from him because of [his]
constantly working calculus problems in his head as soon as awake,
while driving car, sitting in living room, and so forth, and that his
one hobby was playing his African drums. His ex-wife reportedly
testified that on several occasions when she unwittingly disturbed
either his calculus or his drums he flew into a violent rage, during
which time he choked her, threw pieces of bric-a-brac about and
smashed the furniture. -- FBI