in reply to Strange "undefined value as an ARRAY reference" error

I haven't investigated it fully, but...
I used a package rather than lexical array to avoid "Variable ... will not stay shared ..." warnings

You shouldn't have this problem if your code is as you posted. Please try it using my. Results will be different, and examining the differences should be illuminating.

I don't quite understand why it is so, but there seems to be a problem with the following syntax when the sub returns undef:

print qq{pat_mc : @{ [ pat_mc() ] }\n};
Lastly: You should definitely be checking $reference for a valid value before subjecting it to any further operations. If you have pat_mc throw an explicit error (die) when $reference is undef, you'll probably get a much more useful diagnostic than letting it fail inside a bizarro regex code pattern. :-)

Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.

Replies are listed 'Best First'.
Re^2: Strange "undefined value as an ARRAY reference" error
by ikegami (Patriarch) on Jan 08, 2009 at 01:49 UTC
    The OP is right. If you use a variable in a Perl regexp block, and that variable is defined outside the regexp, it's gotta be a package variable. Regexps acts as captures when they are compiled.
    sub foo { my ($x) = @_; '' =~ /(?{ print "$x\n" })/; } foo('a'); foo('b');
    Variable "$x" will not stay shared at (re_eval 1) line 2. a a

    The warning was added in 5.10, but the problem has always existed.

    However, the OP should have localized the variables. Bonus, this initializes @common_letters to ().

    local our @common_letters; local our @wordsCopy = @words;