in reply to Best way to take reference to an array \ or []

They do different things...

use 5.010; use strict; use warnings; my @array = ('a' .. 'c'); sub get_ref1 { \@array }; # returns a ref to @array sub get_ref2 { [@array] }; # creates an anonymous array and returns r +ef to it my $ref1 = get_ref1(); push @$ref1, 'd'; say "(@$ref1) and (@array) - note 'd' has been added to original array +"; my $ref2 = get_ref2(); push @$ref2, 'e'; say "(@$ref2) and (@array) - note 'e' has NOT been added to original a +rray";
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Best way to take reference to an array \ or []
by vinoth.ree (Monsignor) on Jan 22, 2013 at 12:33 UTC

    Ok. thats why I am getting the array ref correctly on using [] and "my" declares the @temp array each time so \ works here!!! Correct me if I am wrong!

      Note that your code, especially the line

      return \%user_details;
      will return a hash reference.

        Hi muba

        Thanks for that, I corrected in my code, updating the post now