Use named parameters:

#!/usr/bin/perl use warnings; use strict; update_table( name => 'test_name', phone => 'test_phone', city => 'test_city', country => 'test_country' ); update_table( name => 'test_name', phone => 'test_phone', country => 'test_country' ); update_table(name => 'test_name', city => 'test_city'); sub update_table { my (%params) = @_; die "name parameter is required by update_table ()\n" if !exists $params{name}; my @fieldNames = grep {$_ ne 'name'} keys %params; my @fields = map {"$_ = *"} @fieldNames; my $sql = "UPDATE table SET "; my @values = (@params{@fieldNames}, $params{name}); $sql .= join ', ', @fields; $sql .= " WHERE name = '*'"; print "$sql [values are @values]\n"; return 1; }

Prints:

UPDATE table SET country = *, city = *, phone = * WHERE name = '*' [va +lues are test_country test_city test_phone test_name] UPDATE table SET country = *, phone = * WHERE name = '*' [values are t +est_country test_phone test_name] UPDATE table SET city = * WHERE name = '*' [values are test_city test_ +name]

Note too that this uses place holders which should always be used to avoid quoting issues (and injection attacks for untrusted data sources).

True laziness is hard work

In reply to Re: Flexible Update SQL by GrandFather
in thread Flexible Update SQL by bichonfrise74

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.