Meh, easy mistake to make.

Instead of using the concat operator (ie. '.'), you can escape the dollar (note: the single quotes on your href keys are superfluous here):

"\$.$row->{name}"

...or to be even more safe, you can use a special trick to interpolate a variable inside of a string, if it gets more complex. This way, perl knows where the variable is, and the other chars are:

my $x = 'sent'; print "\$.${x}ence"; # output: $.sentence

With a hashref, this looks a lot different and is a lot more convoluted*:

"\$.${\$row->{name}}ence"

In cases like that, I much prefer sprintf(). It's always a dead reliable way to concoct strings:

sprintf("\%.%s", $row->{name});

That way, you can even do things like this:

my $row = {name => 'sent'}; my $thing = sprintf("\$.%sence", $row->{name}); # output: $.sentence

For readability, I like to write my db queries so that they're broken down logically, like this:

my $something_query = qq{ SELECT COUNT(*) FROM Person WHERE Account_idAccount = ? AND JSON_EXISTS(custom, ?) };

Then, if the variables I need to feed into the engine are anything but single scalars in a list:

my @array = $dbh->selectrow_array( $something_query, undef, $account, sprintf( "\$.%s", $row->{name} ) );

There's no reason to feel silly for little things like this. Even the most experienced programmers bang their heads against their desks from time-to-time for seemingly simple issues. The good thing is you were humble enough to reach out, and that led to a successful conclusion. Keep doing that. Future users of your software will appreciate your willingness to ask others for help when you can't quite solve something. Much better to be humble than it is to try to broom the cockroach under the carpet. This quality will lead to your users trusting you. Trust me.

*- Because you have to reference a reference to a dereferenced reference (...and I still don't think I understand that correctly ;) )

In reply to Re^3: DBI and JSON fields by stevieb
in thread DBI and JSON fields by Bod

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.