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

How to create anonymous scalar reference ?
I used to create like this :
my ($ref ) = \[]->[0];
Is there any other way to do the same ?

Replies are listed 'Best First'.
Re: anonymous scalar reference
by ikegami (Patriarch) on Nov 26, 2008 at 07:01 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.

Re: anonymous scalar reference
by cdarke (Prior) on Nov 26, 2008 at 08:02 UTC
    my $ref = \do{my $some_scalar};
    A reason to create such a reference is for inside-out objects:
    use Scalar::Util qw(refaddr); ... my $this = bless \do{my $some_scalar}, $class; my $key = refaddr $this;
Re: anonymous scalar reference
by ccn (Vicar) on Nov 26, 2008 at 06:55 UTC

    What is the reason to create such references? It can be done with autovivification when needed.

    my $ref; $$ref = ''; print $ref;
Re: anonymous scalar reference
by tinita (Parson) on Nov 26, 2008 at 16:14 UTC