FAQ
Hi list,

This question has to do with the ref() built-in function and its return values.

I see that one possible return value is 'REF', which I am inferring to mean that the argument given to ref() is a reference to a scalar which is itself a reference.  However, I have not seen this documented; could someone please (1) confirm or refute this (2) clarify if necessary and (3) point me to the relevant documentation.  (The perlfunc page for ref() just lists possible return values, and not their meanings.)

TIA.



__________________________________________________________________
New! Unlimited Access from the Netscape Internet Service.
Beta test the new Netscape Internet Service for only $1.00 per month until 3/1/04.
Sign up today at http://isp.netscape.com/register
Act now to get a personalized email address!

Netscape. Just the Net You Need.

Search Discussions

  • Drieux at Dec 24, 2003 at 7:37 pm
    On Dec 24, 2003, at 11:24 AM, mcdavis941@netscape.net wrote:
    [..]
    However, I have not seen this documented; could someone please (1)
    confirm or refute this (2) clarify if necessary and (3) point me to
    the relevant documentation.  (The perlfunc page for ref() just lists
    possible return values, and not their meanings.)
    what you will want to read is

    perldoc perlref

    eg:
    $scalarref = \$foo;
    $arrayref = \@ARGV;
    $hashref = \%ENV;
    $coderef = \&handler;
    $globref = \*foo;

    the basic refs...

    so I think your question is:

    given
    my @array = qw/bob ted carol alice/;
    my $arrayref = \@array;
    my $refref = \$arrayref;

    then
    ref(@array) will return empty as @array is not a reference
    ref($arrayref); will return "ARRAY" as it is an array ref
    ref($refref); will return "REF" because it is a reference to something
    that we did not recurse on.

    while of course ref($$refref); will get us back to the thing
    that $refref references... and of course one can get back
    to the array in itself with

    @$$refref

    HTH.

    ciao
    drieux

    ---
  • Wiggins d Anconia at Dec 24, 2003 at 8:06 pm

    On Dec 24, 2003, at 11:24 AM, mcdavis941@netscape.net wrote:
    [..]
    However, I have not seen this documented; could someone please (1)
    confirm or refute this (2) clarify if necessary and (3) point me to
    the relevant documentation. (The perlfunc page for ref() just lists
    possible return values, and not their meanings.)
    what you will want to read is

    perldoc perlref

    eg:
    $scalarref = \$foo;
    $arrayref = \@ARGV;
    $hashref = \%ENV;
    $coderef = \&handler;
    $globref = \*foo;

    the basic refs...

    so I think your question is:

    given
    my @array = qw/bob ted carol alice/;
    my $arrayref = \@array;
    my $refref = \$arrayref;

    then
    ref(@array) will return empty as @array is not a reference
    ref($arrayref); will return "ARRAY" as it is an array ref
    ref($refref); will return "REF" because it is a reference to something
    that we did not recurse on.

    while of course ref($$refref); will get us back to the thing
    that $refref references... and of course one can get back
    to the array in itself with

    @$$refref
    Out of curiousity, why/when *in Perl* would you take a reference to
    something that holds a reference? And, "how deep does the well go?"
    (how far will Perl take the above indirection?)... I suppose I could
    just test, but I am rather lazy...

    http://danconia.org

    --
    Boycott the Sugar Bowl! You couldn't pay me to watch that game.
  • Drieux at Dec 24, 2003 at 9:03 pm
    On Dec 24, 2003, at 12:05 PM, Wiggins d Anconia wrote:
    [..]
    Out of curiousity, why/when *in Perl* would you take a reference to
    something that holds a reference?
    Good question, let me know if you find a gooder answer.
    But clearly the fact that it can detect that a given
    scalar is a reference to a reference suggests that
    the keepers of ref() were aware that there may well
    be time when that too would be a good enough fix
    for something.
    And, "how deep does the well go?"
    [..]

    my expectation is that it can probably go as deep
    as you have room for memory...

    so you have some place to start, Xmas, et al
    <http://www.wetware.com/drieux/pbl/perlTrick/refs/ref_gag.plx>

    but can I suggest that you think seriously
    about therapy.....

    ciao
    drieux

    ---
  • Randal L. Schwartz at Dec 24, 2003 at 9:50 pm
    "Wiggins" == Wiggins D Anconia writes:
    Wiggins> Out of curiousity, why/when *in Perl* would you take a reference to
    Wiggins> something that holds a reference? And, "how deep does the well go?"
    Wiggins> (how far will Perl take the above indirection?)... I suppose I could
    Wiggins> just test, but I am rather lazy...

    For the same reason you might take a reference to a scalar
    otherwise... indirection.

    For example, I might have

    my $active_table = \@table_one;

    and then I want to call a subroutine to decide a new active table:

    adjust_table(\$active_table);

    sub adjust_table {
    my $table_ref = shift;
    $$table_ref = rand(2) > 1 ? \@table_one : \@table_two;
    }

    I can't change $active_table except by reference.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
  • Wiggins d'Anconia at Dec 29, 2003 at 12:03 am

    Randal L. Schwartz wrote:
    "Wiggins" == Wiggins D Anconia <wiggins@danconia.org> writes:

    Wiggins> Out of curiousity, why/when *in Perl* would you take a reference to
    Wiggins> something that holds a reference? And, "how deep does the well go?"
    Wiggins> (how far will Perl take the above indirection?)... I suppose I could
    Wiggins> just test, but I am rather lazy...

    For the same reason you might take a reference to a scalar
    otherwise... indirection.

    For example, I might have

    my $active_table = \@table_one;

    and then I want to call a subroutine to decide a new active table:

    adjust_table(\$active_table);

    sub adjust_table {
    my $table_ref = shift;
    $$table_ref = rand(2) > 1 ? \@table_one : \@table_two;
    }

    I can't change $active_table except by reference.
    A worthy answer, but isn't this just a style issue based on preference,
    aka maybe to make it look more like a C idiom, rather than a need (which
    is why I emphasized "in Perl" originally)? For instance, it seems more
    Perl'ish to have your function return the reference to
    table_one/table_two rather than calling the function in void context and
    passing the location of where you want the reference set, that seems
    very C'ish (from what little C I have seen).

    In any case, was just seeing what the group could propose, thanks!

    http://danconia.org

    p.s. I am out for a week, don't suppose I will find a computer in
    Cancun, since I won't be looking!!
  • Shawn McKinley at Dec 29, 2003 at 2:59 am

    -----Original Message-----
    From: Randal L. Schwartz
    Sent: Wednesday, December 24, 2003 3:50 PM
    To: beginners@perl.org
    Subject: Re: the ref() function: what does it mean when ...

    "Wiggins" == Wiggins D Anconia <wiggins@danconia.org> writes:
    Wiggins> Out of curiousity, why/when *in Perl* would you take a
    Wiggins> reference to something that holds a reference? And,
    "how deep
    Wiggins> does the well go?" (how far will Perl take the above
    Wiggins> indirection?)... I suppose I could just test, but I
    am rather
    Wiggins> lazy...

    For the same reason you might take a reference to a scalar
    otherwise... indirection.

    For example, I might have

    my $active_table = \@table_one;

    and then I want to call a subroutine to decide a new active table:

    adjust_table(\$active_table);

    sub adjust_table {
    my $table_ref = shift;
    $$table_ref = rand(2) > 1 ? \@table_one : \@table_two;
    }

    I can't change $active_table except by reference.
    My question would be, why force the reference issue at all when
    you could do something like:

    my $active_table = \@table_one;

    $active_table=adjust_table();

    sub adjust_table {
    return(rand(2) > 1 ? \@table_one : \@table_two);
    }

    Shawn

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedDec 24, '03 at 7:25p
activeDec 29, '03 at 2:59a
posts7
users5
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase