Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

I've written a module which has an array as one of its arguments to new();
$x = My::Module->new(){ foo => 'bar', baz => qw( bax bof ) }
and at some point in My::Module, I have to iterate through them:
foreach ( @{ $self->{'baz'} } ) { # some code }
this works on one server ("v5.8.4 built for i386-linux-thread-multi") but not on another ("v5.8.8 built for i686-linux"). I get the error "Can't use string ("bax") as an ARRAY ref while "strict refs" in use".

It appears that the qw isn't automagically making an anonymous array? That somehow only the first item is making it in, as a scalar?

I figured out that it'll be just fine if I change

$x = My::Module->new(){ foo => 'bar', baz => qw( bax bof ) }
to
$x = My::Module->new(){ foo => 'bar', baz => [ 'bax' , 'bof'] }
but what's going on?


($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Anonymous array with qw() doesn't always work?
by Joost (Canon) on Jun 13, 2007 at 13:25 UTC

      What's with all the new(){ ... } constructs? Unless I'm missing something, that's a syntax error.

      -derby