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

i'm sure i'm overlooking something really trivial, but i can't understand what's happening...hoping someone can help (and forgive the elementary nature of this post!!) my code looks like:
1 while (my $dbpass = $sth->fetchrow_arrayref) 2 { 3 ($UIDholder, $LNholder) = @{$dbpass}; 4 } 5 print p($UIDholder); #checking value of UIDholder 6 if (length $LNholder == 0) { 7 print p($UIDholder); #checking value of UIDholder 8 print p("Please continue on to the next page to complete the r +egistration process."); 9 print '<form action="ti_newprofile.pl">'; 10 print '<input type="hidden" name="UIDholder" value="$UIDholder" +>'; 11 print '<input type="submit" value="Continue"></form>';
and the source code for the resulting page looks like this:

<form action="ti_newprofile.pl">
<input type="hidden" name="UIDholder" value="$UIDholder">'
<input type="submit" value="Continue">'
</form>

when i print $UIDholder at lines 5 and 7, it holds the correct value.....why is the reference printing at line 10? i've tried both value=$UIDholder and value="UIDholder", and neither works.

again, my apologies for the simplistic nature of this question - i'm probably just too tired!!

Replies are listed 'Best First'.
Re: scalar ref losing it's value?
by mildside (Friar) on May 05, 2006 at 06:11 UTC
    It's the single quotes that are doing it. Variables are not interpolated within single quotes.
    Try changing the line to:
    print "<input type=\"hidden\" name=\"UIDholder\" value=\"$UIDholder\"> +";

    mildside

      Or slightly clearer imo:

      print qq{<input type="hidden" name="UIDholder" value="$UIDholder">};
      this is good place for here-docs, too:
      print <<EOS; <form action="ti_newprofile.pl"> <input type="hidden" name="UIDholder" value="$UIDholder"> <input type="submit" value="Continue"></form> EOS
Re: scalar ref losing it's value?
by bart (Canon) on May 05, 2006 at 07:04 UTC
    Why are you first assigning to two variables in a loop, and only use those values after the loop has finished? That way, you'll only ever be using the values set in the last loop.

    You probably want to put the rest of the code in the loop body, if the idea is to test all data pairs that were fetched.

      You probably want to put the rest of the code in the loop body

      I thought that at first, but then I figured his query returns at most one row. Using a loop to fetch one item is very misleading. He should probably have used

      my $dbpass = $sth->fetch; ($UIDholder, $LNholder) = @{$dbpass}; ...
      or
      my $dbpass = $sth->fetch or die("Bad data. XXX not found\n"); $sth->fetch and die("Bad data. Multiple XXX in table YYY\n"); ($UIDholder, $LNholder) = @{$dbpass}; ...
        Even if so, I wonder what it's supposed to do if he gets more than one row. Now he just ignores every one except for the last. Yours will ignore everything but the first, I think.
        actually, i was only trying to get one row back, so thank you for the improved code on accomplishing that....i still get very disordered in my thinking about dealing with arrays...i was trying a few things, and i kept getting back the array ref, rather than the array itself.
Re: scalar ref losing it's value?
by japhy (Canon) on May 05, 2006 at 14:00 UTC
    Simply put: you're printing $UIDholder in a single-quoted string, which does not expand variables. Also, you're using the word "reference" wrong. Don't use it like that. Bad mojo.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      please educate me about the meaning of "reference"...isn't the entity "$UIDholder" the scalar reference to the value which in this case was an integer?? what's wrong about saying that the reference was printing in my incorrectly quoted code, when, in fact, it was the reference which was printing in my HTML??

      i admit, i often have trouble with referencing and dereferencing, but it's getting better :-D

        $UIDholder is just a variable name. If you want a reference to it, you do this:
        my $UIDholder = "some value"; my $var_ref = \$UIDholder; print "UIDHolder: $UIDholder\n"; print "reference: $var_ref\n";

        This gives the output:
        UIDholder: some value reference: SCALAR(0x8160e8c)

        Note that the memory location of your reference may be different from mine. SCALAR indicates the type of variable the reference points to.
        To use the reference, you have to 'dereference' it. For instance, to print the actual value the variable holds by using the reference instead of the variable name, throw an extra $ sign in front of the $var_ref variable name:
        print "Actual value of variable: $$var_ref\n";


        I hope this helps.


        "Peace, love, and Perl...well, okay, mostly just Perl!" --me

        Apprentice