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
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 besub f { my $ref=shift; ... $$ref=4711; }
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.# 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
UPDATE:Typo in NULLREF corrected
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Anonymous scalar ref revisited
by cdarke (Prior) on Apr 06, 2010 at 11:03 UTC | |
by rovf (Priest) on Apr 06, 2010 at 11:36 UTC | |
|
Re: Anonymous scalar ref revisited
by almut (Canon) on Apr 06, 2010 at 11:58 UTC | |
|
Re: Anonymous scalar ref revisited
by BrowserUk (Patriarch) on Apr 06, 2010 at 12:02 UTC | |
|
Re: Anonymous scalar ref revisited
by dk (Chaplain) on Apr 06, 2010 at 11:32 UTC | |
by rovf (Priest) on Apr 06, 2010 at 11:46 UTC | |
by dk (Chaplain) on Apr 07, 2010 at 08:50 UTC | |
|
Re: Anonymous scalar ref revisited
by LanX (Saint) on Apr 06, 2010 at 10:51 UTC | |
by rovf (Priest) on Apr 06, 2010 at 11:24 UTC |