Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: How to add quotes to comma separated values in a String (updated)

by haukex (Archbishop)
on Feb 13, 2018 at 21:32 UTC ( [id://1209087]=note: print w/replies, xml ) Need Help??


in reply to Re^4: How to add quotes to comma separated values in a String (updated)
in thread How to add quotes to comma separated values in a String

You're making a lot of assumptions about the data, whereas I assumed that "CAT,DOG,BIRD,COW" was just an example and not the actual input data, so we really don't know what it'll be ("be liberal in what you accept"). Knowing how to do it in plain Perl is of course useful, but personally I'd prefer the first solution people come across to be a robust one - hence the somewhat dogmatic statement, but hopefully for a good reason ;-) I also agree entirely with Your Mother's posts.

If all your assumptions hold, then sure, it's fine to use plain Perl, but even then I would have written something like the following - just one more line of code to protect against the input changing unexpectedly:

my $input = "CAT,DOG,BIRD,COW"; $input =~ /\A\w*(?:,\w*)*\z/ or die "invalid input format"; my $str = join ',', map { "'$_'" } split /,/, $input;

I did assume that the OP, since they are doing work with a database, will have a $dbh lying around. Note that our two pieces of code really aren't that different - only a couple more characters for extra protection :-) Also note that using the database driver for quoting should take care of possible quoting differences between databases.

my $str = join ',', map { $dbh->quote($_,'VARCHAR') } @values; my $str = join ',', map { "'$_'" } @values;

Replies are listed 'Best First'.
Re^6: How to add quotes to comma separated values in a String (updated)
by Laurent_R (Canon) on Feb 13, 2018 at 23:16 UTC
    Hi haukex,

    first, I'm really sorry, I should not have used the work "dogmatic." I understand it might be considered a bit derogatory, and I really did not mean that.

    Then, whether you're using the DBI quote subroutine or the core Perl solutions I suggested, you need to validate the input data beforehand anyway if it's coming from an external source.

    I was perfectly happy with the solutions you suggested (and I think I said so), I only reacted to the sentence: "this is the very least you should do - don't go and try to quote the strings yourself."

    Finally, I really think that somebody learning Perl and not knowing how to quote a bunch of words should learn how to do it in pure Perl first.

    I have seen recently at our job some interns knowing apparently almost everything about the Symphony framework and not being really able to write five lines of correct PHP (I even had to help them and fix their code although my knowledge of PHP is close to nothing).

    With all due respect to what Your Mother and you said, I think developers should learn to do things the hard way in the language they use before using props and crutches available in packages, libraries, modules, and frameworks. Well, maybe I am wrong, after all, I must admit that don't know how the library that I use computes the sin function. Perhaps it is just me being too old school.

      No worries, no offense taken :-) On a different day, I might have posted both solutions.

      Coming from the perspective of learning Perl, I completely agree with you that learning the basics first is the better way to go. When I learned e.g. the Fourier transform, our professor first had us do it entirely by hand for a while, before we learned how to use the tables. The tables are of course much easier to use, but I still think it was important to know the "hard way" of doing it.

      Coming from the perspective of someone Googling and looking for a quick solution to copy-and-paste, I think showing the more robust one first and making clear that it's the "better" solution is better. After all, maybe someone will implement Stack Overflow Autocomplete for Perl someday ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-19 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found