in reply to Re^2: anonymous scalar reference
in thread anonymous scalar reference

Anonymity is not the goal. The ability to generate as many references as needed is.
my @refs; for (1..10) { push @refs, \my $foo; } print "$_\n" for @refs;
SCALAR(0x18317d8) SCALAR(0x23605c) SCALAR(0x236098) SCALAR(0x236d88) SCALAR(0x1831838) SCALAR(0x1831808) SCALAR(0x1831868) SCALAR(0x1831880) SCALAR(0x1831898) SCALAR(0x18318b0)

You'll might see \do { my $foo } and do { \my $foo }, but the extra scope is almost always overkill.

Replies are listed 'Best First'.
Re^4: anonymous scalar reference
by JadeNB (Chaplain) on Nov 26, 2008 at 15:57 UTC
    I think that saying “the extra scope is almost always overkill” is a little disingenuous—your second example only works1 because of the extra scope. Specifically, if you tried to unroll this for loop, then you'd have to either think up a bunch of extra names, or get warned.

    1 By which I mean, only works silently under warnings.
    UPDATE: Despite typing it out myself, I didn't register the ‘almost always’, and so was reacting as if the statement was unqualified. I'm sorry about that.

      your second example only works1 because of the second scope

      What do you call the second example? The way I see it, I only posted one.

      Specifically, if you tried to unroll this for loop, then you'd have to either think up a bunch of extra names, or get warned.

      But why would you do that. I can't imagine any scenario where you'd do that. Can you come up with a real world example where you need to generate more than one such reference in the same scope? If there was such an case, it seems to me that having different names for different variables would be a good thing.

        What do you call the second example? The way I see it, I only posted one.
        Sorry, my wording was pretty bad. I was referring to your examples of a single reference to an ‘anonymous’ scalar and of ten references to ‘anonymous’ scalars. The first one genuinely has no extra scope, whereas the second one has the for introduce the extra scope.

        But why would you do that. I can't imagine any scenario where you'd do that. Can you come up with a real world example where you need to generate more than one such reference in the same scope?
        The only common use of anonymous scalars that I know is as blessees when the contents don't matter. I've occasionally used this to produce ‘poor man's mixins’:
        bless [ bless \do { my $anon } => A, bless \do { my $anon } => B ] => +C;
        Now package C can delegate calls to methods that should be handled by package A or B. I'm not sure if that's a good usage, though.

        If there was such an case, it seems to me that having different names for different variables would be a good thing.
        I agree that different names for the references is good, but avoiding introducing an extra scope requires different names for the referents. That is, I'd have to write something like
        bless [ bless \my $anon_in_A => A, bless \my $anon_in_B => B ] => C
        instead.