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
In reply to Anonymous scalar ref revisited by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |