in reply to anonymous scalar reference

my $ref = \my $foo;

Replies are listed 'Best First'.
Re^2: anonymous scalar reference
by pobocks (Chaplain) on Nov 26, 2008 at 07:37 UTC
    How is that anonymous?
    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      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.

        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.