Re: Rewrite in a more compact way
by moritz (Cardinal) on Nov 27, 2008 at 11:34 UTC
|
my $str = q{CALL DB_PROC_FOO(} . join(',', ('?') x $n) . ')';
(assuming that you don't care if $n is zero afterwards) | [reply] [d/l] [select] |
|
|
my $str = 'CALL DB_PROC_FOO(' . join(',', ('?') x $n) . ')';
;-)
A user level that continues to overstate my experience :-))
| [reply] [d/l] |
|
|
Since andreas1234567 asked for a compact way, I also used a compact way to quote strings.
I know that PBP encourages q{..} and discourages '...' on "noisy" strings, but it's one of the things I don't like visually. (I do resort to other quoting constructs if it saves me backslashes for escaping, which are usually a bit confusing).
Note that your version also isn't consequent, because it still uses ',' instead of q{,}.
| [reply] [d/l] [select] |
|
|
Re: Rewrite in a more compact way
by BrowserUk (Patriarch) on Nov 27, 2008 at 12:28 UTC
|
my $str = sprintf 'CALL DB_PROC_FOO( %s )', join ',', ('?') x $n;
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
I'd reach for sprintf too. But I'd put my one line on seven. Does that count? :-)
my $str = sprintf(
q{CALL DB_PROC_FOO(%s)},
join(
q{,},
(q{?}) x $n,
),
);
I like to use parens in these cases to have something to pin the ; to. And if there are other joins, splits etc. it stops perl getting confused about where lists start and end.
| [reply] [d/l] |
|
|
Well, it's in the eye of the beholder, (and I don't mean 'beauty' :), but that's just verbosity for it's own sake (IMO).
The most that is required here (and only then if you insist on sticking with the limitations of 1970s VTs), is:
my $str = sprintf(
'CALL DB_PROC_FOO(%s)',
join ',', ( '?' ) x $n
);
I'd love to see the PBP justifiction for this absurd practice of always replacing simple string constants with verbose and confusing q{} constructs. (Damn! But has that book got a lot to answer for!).
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
|
|
|
Re: Rewrite in a more compact way
by ysth (Canon) on Nov 27, 2008 at 11:35 UTC
|
my $str = q{CALL DB_PROC_FOO(} . join(q{,}, (q{?}) x $n) . q{)};
In list context, ()x is the list repetition operator. It's one of the few cases in perl where
() does something more than adjust precedence or delimit function parameters.
| [reply] [d/l] |
Re: Rewrite in a more compact way
by Corion (Patriarch) on Nov 27, 2008 at 11:36 UTC
|
my @placeholders = ('?') x $n;
my $str = sprintf <<'SQL', join ",", @placeholders;
CALL DB_PROC_FOO(%s);
SQL
Sometimes I also use <<'' instead of <<'SQL' but I find that I tend to insert blank lines into my SQL to structure it better, so that's a practice that leads to inconvenient errors.
Maybe you want to give SQL::Abstract a try, but in the long run, I found that I ended up fighting SQL::Abstract's ideas of how I should structure my data to get the SQL I want, and it was faster for me to just interpolate a string. | [reply] [d/l] [select] |
Re: Rewrite in a more compact way
by Bloodnok (Vicar) on Nov 27, 2008 at 12:07 UTC
|
In addition to the excellent answers provided by corion, ysth & moritz, I may I suggest the use of a different delimiter for the 'q' operator - it took me a while to read
my $str = q{CALL DB_PROC_FOO(} . join(q{,}, @qm) . q{)};
...the braces and parens looked alike until I looked really close ... compare with
my $str = q/CALL DB_PROC_FOO(/ . join(q/,/, @qm) . q/)/;
where confusion of braces and parens is, IMO, somewhat reduced.
A user level that continues to overstate my experience :-))
| [reply] [d/l] [select] |
Re: Rewrite in a more compact way
by Narveson (Chaplain) on Nov 28, 2008 at 04:28 UTC
|
I have found that the easiest version for me to read (and understand when I come back to it later) is an interpolated array:
my @qm = ('?') x $n;
my $str = do {local $" = q{,}; "CALL DB_PROC_FOO(@qm)"};
| [reply] [d/l] |