A Perl Chicken-and-egg problem

Madison Kelly linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org
Sat Jun 21 18:58:33 UTC 2008


It is very common for module's methods to return array and hash 
references instead of a copy of an array or hash to save memory and 
improve speed. Try:

my $A = myobj::files::list(params); // same declaration as in the docs
foreach $x (@{$A}) {
     print "$x\n";
}

Madi

Paul King wrote:
> Hello
> 
> I dabble in Perl occasionally, and when I have to use modules or objects
> in Perl, it is a dusty corner for me where the documentation is normally
> unhelpful.
> 
> I need to use an array which is in one of these modules -- the module
> name is not important -- where the actual array is pointed to by the
> variable it passes to me. For example, 
> 
> my @A = myobj::files::list(params); // same declaration as in the docs
> foreach $x (@A) {
>     print "$x\n";
> }
> 
> yields nothing except a pointer to an array -- something like
> 
> ARRAY(#A1B2C3D4)
> 
> in the output. This causes me to have to alter my code to do this:
> 
> my @A = myobj::files::list(params);
> foreach $x (@A) {
>     for ($i = 0; $i < $max; $i++) {
>          print "$x->[$i]\n";
>     }
> }
> 
> This, I think, gives the output to the array pointed to by $x (I say "I
> think", since my copy of O'Reilly's Programming Perl 2e says that you
> dereference an array using "=>" not "->"). And the reality is worse,
> since the module I am using actually returns a pointer to a
> 2-dimensional array:
> 
> my @A = myobj::files::list(params);
> foreach $x (@A) {
>     for ($i = 0; $i < 16; $i++) {
>          for $j = 0; $j < 16; $j++) { 
>               print "$x->[$i][$j]\n";
>          }
>     }
> }
> 
> This kind of works. I get the output I expect, except for one thing. I
> don't know the array size, so I am hard-coding the upper limit of the
> loops. $#x to find the size doesn't work, since (1) $x merely points to
> an array, and (2) the array is two dimensional. Any suggestions?
> 
> Paul King
> 
> 
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
> 

--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list