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

Is there a simple/elegant way in Perl to create an anonymous scalar reference? There was a discussion on this subject at anonymous scalar reference, but IMHO, the solutions suggested were either not simple, or not anonymous, or both. As a motivation for this question, let's assume that we have some function

sub f { my $ref=shift; ... $$ref=4711; }
which we are not supposed to modify, and that we want to use in a context where we are NOT interested in the value which is stored via $$ref. Possible invocations would be
# Solution 1 my $ref; f(\$ref); # Just don't use $ref afterwards # Solution 2 f(do{\my$x}); # Not really anonymous, since we have to # "invent" a variable name, but at least # blessed by Larry # Solution 3 f(\[]->[0]); # Anonymous, but not elegant # Solution 4 sub NULLREF { \ my $x } f(NULLREF);# Maybe nice if we use this feature often in our # program and use NULLREF consistently, but still # not really anonymous

I know that there are more important problems to solve on this planet, but maybe someone could suggest a really nice improvement over the solutions posted above.

UPDATE:Typo in NULLREF corrected

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Anonymous scalar ref revisited
by cdarke (Prior) on Apr 06, 2010 at 11:03 UTC
    Your Solution 3 can be shorter:
    f(\[]);
    or even
    f(\{});
    Whether it is elegant is in the eye of the beholder. It surprised me that this worked, since the scalar is a reference to a reference, but it can be assigned to and modified, unlike f(\1) which gives a reference but is read-only (but does appear to return different addresses each time called).

    Update: The following also appears to work in this case
    f(\());
    but this is an aberation of the subroutine call interface.
    my $ref = \(); $$ref = 42;
    Gives: Modification of a read-only value attempted
      Your Solution 3 can be shorter:
      f(\[]);
      This is clever! I did not see first that this is really equivalent to my solution 3, but indeed it is. Plus, I find it simple and elegant. Well, a little bit obfuscating maybe, but meeting my requirements. Thanks a lot!

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Anonymous scalar ref revisited
by almut (Canon) on Apr 06, 2010 at 11:58 UTC

    Not quite what you asked, but the shortest variant would be

    f;

    in which case the variable would be autovivified from an undefined value.  Of course, this only works (without warnings) as long as you don't read $ref or $$ref before having assigned it something...

    #!/usr/bin/perl -l use strict; use warnings; sub f { my $ref=shift; $$ref=4711; print "$ref -> $$ref"; } f; __END__ $ ./833010.pl SCALAR(0x62d238) -> 4711

    P.S.: same thing happens in dk's suggestion, btw, when taking a reference to the return value of f (i.e. \f).

Re: Anonymous scalar ref revisited
by BrowserUk (Patriarch) on Apr 06, 2010 at 12:02 UTC

    Looks like your trying emulate Lamda, so:

    f(\_);

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Anonymous scalar ref revisited
by dk (Chaplain) on Apr 06, 2010 at 11:32 UTC
    f(\f)

    (not supposed to be serious, but it's elegant at least :)

      Certainly a funny proposal (though it bails out if the subroutine itself is unnamed, but I admit that this is a border case which we will ignore). Still I'm puzzled how this can work. What is the f in this \f supposed to mean? It can't be a reference to the subroutine f, sind this would be \&f and wouldn't be useful either. Hence it must mean that we apply \ to the result of a parameterless invocation of f, but this shouldn't work either. Which hat did you get this f out from?

      -- 
      Ronald Fischer <ynnor@mm.st>
        That works only because f() itself is so simple, and also because

        my $ref=undef; $$ref=4711;
        works too. That's why it's not serious, but also probably answers your question more deeply - you don't need a special syntax for anonymous scalar-ref, it gets autovivified when necessary.
Re: Anonymous scalar ref revisited
by LanX (Saint) on Apr 06, 2010 at 10:51 UTC
    > Is there a simple/elegant way in Perl to create an anonymous scalar reference?

    does elegant imply as fast as possible?

    if not, why simply call a function scref() returning what you need? ¹

    the thread you linked to, has already other fast options, you haven't listed yet.

    Cheers Rolf

    UPDATE: (¹) just spotted your NULLREF() (was it updated?) going in the same direction, just that your code is wrong, you don't return a ref!

    correction:

    # Solution 4 sub NULLREF { \ my $x } f(NULLREF);
      just spotted your NULLREF() (was it updated?)
      No, was there from the beginning!

      just that your code is wrong, you don't return a ref!
      You're right, my mistake. I'll update it.
      -- 
      Ronald Fischer <ynnor@mm.st>