Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Rewrite in a more compact way

by andreas1234567 (Vicar)
on Nov 27, 2008 at 11:29 UTC ( #726372=perlquestion: print w/ replies, xml ) Need Help??
andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

I use the code below for calling stored procedures with a varying number of arguments.

How can this be written in a more compact way? Ideally I would like the 3 lines of code between the ----------- lines to be written on a single line only:

use strict; use warnings; our $n ||= 10; # ----------- my @qm = (); push @qm, '?' while $n--; my $str = q{CALL DB_PROC_FOO(} . join(q{,}, @qm) . q{)}; # ----------- print $str; __END__ $ perl -swl qm.pl -n=1 CALL DB_PROC_FOO(?) $ perl -swl qm.pl -n=2 CALL DB_PROC_FOO(?,?) $ perl -swl qm.pl -n=3 CALL DB_PROC_FOO(?,?,?)
Update: Thank you all for your time and effort.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Comment on Rewrite in a more compact way
Select or Download Code
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)

      Hi moritz ,

      In the interests of consistency, methinx your snippet should have read

      my $str = 'CALL DB_PROC_FOO(' . join(',', ('?') x $n) . ')';

      ;-)

      A user level that continues to overstate my experience :-))
        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{,}.

Re: Rewrite in a more compact way
by ysth (Canon) on Nov 27, 2008 at 11:35 UTC
Re: Rewrite in a more compact way
by Corion (Pope) on Nov 27, 2008 at 11:36 UTC

    Does the x operator help?

    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.

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 :-))
Re: Rewrite in a more compact way
by BrowserUk (Pope) on Nov 27, 2008 at 12:28 UTC
      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.

        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.
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)"};

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://726372]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-07-24 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls