[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Constants as array refs



On Thu, Mar 23, 2000 at 02:37:23PM -0500, Jeff Boes wrote:
> Okay, this ought to be easy but it's not, and I'm just drawing a blank
> today.
> 
> Given:
> 
>   use constant CARRAY => [1, 2, 3, 4];
> 
> What is the syntax to dereference this constant directly into the list of a
> 'foreach' loop?
> 
> None of these work:
> 
>   foreach (CARRAY)

foreach ([1, 2, 3, 4])

>   foreach (( CARRAY ))

foreach ('CARRAY')

>   foreach (@{ CARRAY })

foreach (@CARRAY)
# presumably an empty list, unless you happen to have an array by that name

>   foreach (@{[ CARRAY })

syntax error

(or foreach ([1, 2, 3, 4]) if the missing ] is a typo.)


Try this instead:

foreach (@{ +CARRAY })


The + tells Perl that this is an expression, not an identifier.


Ronald

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe