htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am setting a var that equals a price. I then use that var as part of another var that includes some html. When it gets stuffed in the database, instead of printing the price it prints the var name. If I print the whole thing to a text file first, then read in the text file and stuff that in the database, it includes a price. I'm doing something like this:

my $price = "50"; my $htmlvar = "<li>The price is \$$price</li>"; my $sth = $dbh->prepare(qq{UPDATE database SET htmlvar=? WHERE ID=?}); $sth->execute($htmlvar, $ID) or die $dbh->errstr; $sth->finish;

What gets written to the dabase is:

<li>The price is \$$price</li>

How can I have the the database read this:

<li>The price is $50<li>
Thanks.

Replies are listed 'Best First'.
Re: Using vars in a var
by GrandFather (Saint) on Nov 02, 2022 at 02:32 UTC

    What you show us is not what you are doing. Are you sure you are using double quotes (") rather than single quotes (') when you set $htmlvar?

    use warnings; use strict; my $price = "50"; print "$_\n" for "<li>The price is \$$price</li>", '<li>The price is \ +$$price</li>';

    Prints:

    <li>The price is $50</li> <li>The price is \$$price</li>

    Maybe you need to give us some code we can actually run that shows the problem if quoting is not the issue? (See I know what I mean. Why don't you? and SSCCE.)

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Sorry, you're right. I'm using single quotes on $htmlvar like this:
      my $price = "50"; my $htmlvar = '<li>The price is \$$price</li>'; my $sth = $dbh->prepare(qq{UPDATE database SET htmlvar=? WHERE ID=?}); $sth->execute($htmlvar, $ID) or die $dbh->errstr; $sth->finish;
      But I've also used double quotes and I get the same result. I guess I don't understand what the difference is in setting it to a var, vs writing it to a file. The following works:
      $prices_file = "/usr/home/user/public_html/site/prices.txt"; open (OUT_FILE, ">".$prices_file) || die "ERROR: *** Cannot open outpu +t file: $prices_file. ***\n"; print OUT_FILE qq~ <li>The price is \$$price</li> ~; close (OUT_FILE); my $htmlvar; open(my $fh, '<', $prices_file) or die "cannot open file $pric +es_file"; { local $/; $htmlvar = <$fh>; } close($fh); my $sth = $dbh->prepare(qq{UPDATE database SET htmlvar=? WHERE ID=?}); $sth->execute($htmlvar, $ID) or die $dbh->errstr; $sth->finish;
      UPDATE: Okay, I tried it again with the double quotes and got it to work so I guess that was where my error was. I was using single quotes. Thanks.
Re: Using vars in a var
by davido (Cardinal) on Nov 02, 2022 at 16:25 UTC

    There is nothing magical about the database portion of this; it's just a distraction. If $htmlvar contains what you want it to, it will contain that whether you print is value or UPDATE it into a text-based field in a database using placeholders and execute(). So set up a test that eliminates the confusion of the database.

    my $price = "50"; my $htmlvar_qq = "<li>The price is \$$price</li>"; my $htmlvar_q = '<li>The price is \$$price</li>'; print "$_\n" for $htmlvar_qq, $htmlvar_q;

    The output will be:

    <li>The price is $50</li> <li>The price is \$$price</li>

    In response to your question someone suggested that you probably were using single quotes. You followed up by stating that you tried with double quotes also, and still had the problem. If that's the case, please provide code that exhibits the incorrect behavior while printing to STDOUT rather than updating into a database. If it really only happens when updating into the database, please provide an example where you are printing to STDOUT and updating into the database, and getting something different in the database than what you see on STDOUT.


    Dave

Re: Using vars in a var
by afoken (Chancellor) on Nov 02, 2022 at 15:41 UTC

    See also Evaluate variable while using

    Update: Misread the question.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)