Dear monks, you may think this program a crazy one, but I think it worth to discuss.

I was trying to fetch data from two different tables into separate hashes. I always use 'strict'. I use DBI's function  bind_columns with the fancy syntax. The code is

my $data = {}; my $SQL = "SELECT * FROM my_table ORDER BY order"; my $sth = $dbh->prepare( $SQL ); $sth->execute() or die DBI->errstr; my %row; $sth->bind_columns( \( @row{ @{ $sth->{ NAME_uc } } } )); $i = 1; while ( $sth->fetch() ){ push @names, $row{ 'NAME' }; delete $row{ 'NAME' }; #this line causes problem a +t 'bind_columns' down there $data->{"data_$i"} = { %row }; $i++; } $sth->finish(); my $second_data = {}; $SQL2 = "SELECT * FROM my_second_table ORDER BY order"; my $sth2 = $dbh->prepare( $SQL2 ); $sth2->execute(); my %row2 = (); $sth2->bind_columns( \( @row2 { @{ $sth2->{ NAME_lc } } } )); # Er +ror due to delete $row{ 'NAME' }; at top $i=1; while ( $sth2->fetch() ){ $second_data->{"data_$i"} = { %row2 }; $i++; } $sth2->finish();
I get the error
"Can't use an undefined value as an ARRAY reference at /myprogram.pl l +ine 23."
The line causing error is
$sth2->bind_columns( \( @row2 { @{ $sth2->{ NAME_lc } } } ));
When I comment line delete $row{ 'NAME' }; everything works fine.

I wonder why it throws this error since first set of variables has NO connection with the second set, except that they use same DB handle.

Other Observations I have made are

Thanks in advance for the help.

--VC



There are three sides to any argument.....
your side, my side and the right side.


In reply to DBI::bind_colums throws error when element of binding hash is deleteed by atemon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.