I could put the data into a Hash but I'm using the data in SQL statements which result in very long, difficult to read, lines.

Using a hash to store the variables could actually make the construction of the SQL statements easier and clearer. If you happen to be building an "insert into my_table ..." sort of statement, a hash makes this very simple to do via a "for" loop -- e.g.:

... my $col_spec = my $val_spec = "("; for my $column ( keys %data ) { $col_spec .= "$column,"; $val_spec .= "$data{$column},"; } $col_spec =~ s/,$/\)/; $val_spec =~ s/,$/\)/; my $sql = "insert into my_table $col_spec values $val_spec";
and of course, there are shorter ways to do the same thing:
my $col_spec = join( ",", sort keys %data ); my $val_spec = join( ",", map { $data{$_} } sort keys %data ); my $sql = "insert into my_table ($col_spec) values ($val_spec)";
Similar methods apply for update and select statements, using a loop to assemble the "col=val" pairs and "where" clause conditions.

If the basic form of the sql statement will always involve the same set of columns and conditions, you should be using the DBI "prepare()" method with parameterized values (using the "?" placeholder, and passing the values as a list to the "execute()" method).

Also, remember that you can use any sort of whitespace as part of an SQL statement, including line-feeds and tabs, so even long constructs can be formatted so that they are not difficult to read.

update: as pointed out in the next reply below, the values being assembled should be quoted if you are not using the parameterized form of sql statement. (Thanks, and ++, blokhead!)


In reply to Re: Creating variables while using 'strict' by graff
in thread Creating variables while using 'strict' by nedals

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.