For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Re: how to obtain a reference to array which is returned by a function









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 Re: how to obtain a reference to array which is returned by a function
Jeff 'japhy' Pinyan

2005-07-25, 9:24 am

On Jul 25, Denis N. said:

> my $selectHandle = $dbh->prepare(...);
> ...
> $selectHandle->execute(....);
> my @data = $selectHandle->fetchrow_array();
> $self->[1] = \$data;


That should be \@data, not \$data.

> my $selectHandle = $dbh->prepare(...);
> ...
> $selectHandle->execute(....);
> $self->[1] = $selectHandle->fetchrow_arrayref();
>
> The second one is not correct because it turned out that fetchrow_arrayref()
> return reference to the same array (as described in man page).


Yeah.

> Is there a syntax for obtaining the reference to the array returned by the
> function without copying it first?


Well, since fetchrow_array() returns a list (not an array, and not an
array ref), the best you can hope for right now is

$self->[1] = [ $selectHandle->fetchrow_array() ];

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Tom Allison

2005-08-02, 4:59 pm


> Well, since fetchrow_array() returns a list (not an array, and not an
> array ref), the best you can hope for right now is
>
> $self->[1] = [ $selectHandle->fetchrow_array() ];
>


How about?

$ary_ref = $sth->fetchrow_arrayref;
push @$self, $ary_ref;
Sponsored Links







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

Copyright 2009 codecomments.com