For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2004 > different type of getting an array reference









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author different type of getting an array reference
Octavian Rasnita

2004-12-27, 8:55 am

Hi all,

Can you please tell me what's the difference between these two types of
getting the reference to @a array?

my $ref = [ @a ];
and
my $ref = \@a;

If I do:
use Data::Dumper;
print Dumper $ref;

Both ways of accesing the @a array with a reference seems to be the same,
because Data::Dumper prints the same thing for $ref.

I need to access an array reference in a module, like...

my $self = [ @a ];
or...
my $self = \@a;

and I don't know if there will be any differences...

Thank you much.

Teddy

John W. Krahn

2004-12-27, 8:55 am

Octavian Rasnita wrote:
> Hi all,


Hello,

> Can you please tell me what's the difference between these two types of
> getting the reference to @a array?
>
> my $ref = [ @a ];


This copies the array to an anonymous array and returns a reference to that
anonymous array. It is equivalent to:

my $ref = do { my @temp = @a; \@temp };


> and
> my $ref = \@a;


This returns a reference to the array.


> If I do:
> use Data::Dumper;
> print Dumper $ref;
>
> Both ways of accesing the @a array with a reference seems to be the same,
> because Data::Dumper prints the same thing for $ref.
>
> I need to access an array reference in a module, like...
>
> my $self = [ @a ];
> or...
> my $self = \@a;
>
> and I don't know if there will be any differences...


It depends on the scope of @a.


John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com