Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Why was it neccessary to pass a DBI handler by reference?

by neuroball (Pilgrim)
on Jan 28, 2004 at 15:54 UTC ( [id://324692]=note: print w/replies, xml ) Need Help??

in reply to Why was it neccessary to pass a DBI handler by reference?

It's one of the good/bad things about perl. You get what you ask for.

When you passed your handler just through the variable, I.e. $dbh, you passed the scalar value of $dbh to the hash.

When you passed your handler through a reference, i.e. \$dbh, you passed the memory position of your handler to the hash

So, in the first case you have a scalar value, which is in no way connected to the handler, and in the second case you have a memory address which gives you access to the handler.

It's the strange thing about references. I always make a note in my mind that my finger, which points at the moon, isn't the moon. Yet, if you followed the direction my finger is pointing at, you would surely find the moon.

Sorry when the finger/moon analogy is a bit fuzzy.


Replies are listed 'Best First'.
Re: Re: Why was it neccessary to pass a DBI handler by reference?
by kudra (Vicar) on Jan 28, 2004 at 16:21 UTC
    Don't worry, your moon analogy is fine, but I already know the difference between a reference and a scalar value. My question has more to do with the peculiarities of DBI and why it doesn't act like I expect.

    See this small example program:

    use warnings; use strict; use Foo; use Bar; my $foo = new Foo; my $bar = new Bar($foo); $foo->x("from program"); print "(app) Memory: $foo, X: ".$foo->x()."\n"; $bar->show(); isn't very interesting:
    package Foo; sub new { my $name = shift; bless { x => 'from Foo' }, $name; } sub x { my ($self, $arg) = @_; $$self{x} = $arg if ($arg); return $$self{x}; } 1;
    And just stores some foo:
    package Bar; sub new { my $name = shift; my $foo = shift; bless { foo => $foo }, $name; } sub show { my $self = shift; my $foo = $$self{foo}; print "(bar) Memory: $foo, X: ".$foo->x()."\n"; } 1;

    This example is almost exactly like what I wanted to do, except that I'm using a makeshift module instead of DBI.

    The results of the memory test will of course depend upon the execution, but both times the memory location is the same. That wouldn't happen after I 'forced a copy' by changing one of the values in $foo if I didn't have a reference.

    I hope this clarifies my question.

      Okay, now I understand your problem... but I have to say that I am totally out of my depth.

      I will put my head into some pods and see what happens...

      Nothing to see here but an emberassed monk... go on...


Re: Why was it neccessary to pass a DBI handler by reference?
by Abigail-II (Bishop) on Jan 28, 2004 at 16:12 UTC
    That doesn't make any sense to me. \$dhb is also a scalar value, and a database handler is already a reference.


      Hm... following the logic of the above (if I am not mistaken): I am (a) wrong, and (b) kudra needed to have a reference to a reference???

      Not to be a pain in your neck, but could you explain a bit more what you meant? For example, take the following code:

      #!/usr/local/bin/perl use warnings; use diagnostics; use strict; my $x = "Just another scalar"; my $y = \$x; print $x."\n"; print $y."\n";

      Am i totally of base with this??? Or am I missing something very basic?


        kudra needed to have a reference to a reference???
        Maybe, maybe not. I can't make heads nor tails from kudra's posting. Some vital information, namely the code in the modules One and Two is missing. I'm not going to speculate what's going on.
        For example, take the following code:
        Your code produces:
        Just another scalar SCALAR(0x8191618)
        But what about it? Why do you post that code. Does the result surprise you? Are you trying to make a point?
        but could you explain a bit more what you meant?
        What I meant was every reference is a scalar. There are no exceptions to this rule. What I also meant was that the $dbh in kudra's original post is also very likely to be a reference. Your post seem to imply that you think scalars and references are different things. So, I have two points: 1) references are scalars. 2) in both forms, a reference is passed as second argument to the news anyway.


        Or am I missing something very basic?

        I think you might be conflating a stringified scalar reference with scalars in general. Scalars in Perl are single-value entities. A string is stored as a scalar. A reference is stored as a scalar. These bear no inherent relation to eachother, though. One can have [among other things] (1) a plain string, or (2) a reference to a string, or (3) a reference to something else. All of these are scalars. Only (2) would necessarily print "SCALAR(...)" when stringified, though -- that is a reference thing, not a scalar thing. (I say 'necessarily' because while (3) might print "SCALAR(...)", it could also print "ARRAY(...)", "HASH(...)", or any number of other things, depending on what it's a reference to.)

      As far as I remember, \$dbh is not a scalar. It is a reference, not a scalar. If you print a reference, it doesn't know what to do so it prints out the address value, it doesn't mean it is a scalar. (Of course I may be wrong but I'm pretty sure I am not)
        Considering that arrays and hashes can only contain scalars, how do you construct multi-dimensional datastructures?

        The first paragraph of the description section of 'man perlref' says Any scalar may hold a hard reference. (Hard reference is used here to indicate it's not a soft- or symbolic reference). If references aren't scalars, what does that sentence mean?

        $ perl -MDevel::Peek -wle 'my $dbh = "foo"; Dump \$dbh' SV = RV(0x8194068) at 0x817cca0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x817cd54 SV = PV(0x817cf90) at 0x817cd54 REFCNT = 2 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x818f688 "foo"\0 CUR = 3 LEN = 4
        Devel::Peek thinks that \$dbh is an SV, which stands for scalar value.

        So, what is it that makes you think that references aren't scalars? Do you have documentation quotes, code fragments, or pointers to the source that back up your claim?


Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-06-14 12:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.