in reply to The weirdest problem with undef

In this case your best bet would be to declare my (@array1, @array2) before the for loop if you want them to be available to your subroutines.

You are not having a problem with undef, you are having a problem variable scoping.

As others have mentioned, you should be using strict - you said it won't help you solve the problem, but how can you solve the problem when you clearly don't understand what the problem is? Using strict will help you figure out what the problem is so you can at least look in the right direction.

Here's what I used to test your code:
#!/usr/bin/perl use strict; foreach my $item ('top', 'heavy', 'fool') { my (@array1, @array2); a_function($item); print "@array1\n"; print "@array2\n"; } sub a_function { my $item = shift; push @array1, 'crap'; push @array2, 'dumb'; }
This gave me errors like this:
Global symbol "@array1" requires explicit package name at ./test_loop. +pl line 18. Global symbol "@array2" requires explicit package name at ./test_loop. +pl line 19. Execution of ./test_loop.pl aborted due to compilation errors.
This clearly tells me that the subroutine is looking for global variables. Declaring them as my inside a loop doesn't create global variables. So moving them to the top, like this, works fine:
... my (@array1, @array2); foreach my $item ('top', 'heavy', 'fool') { ...

Replies are listed 'Best First'.
Re^2: The weirdest problem with undef
by insaniac (Friar) on Dec 22, 2004 at 19:24 UTC
    ah thanks!
    this was a really cool answer, learned much from it. like: test your code in smaller in pieces if you have a problem. i always seem to write too much at once, leaving these error hard to find. but like i really really realy learned today: _always_ use strict; and use warnings;.

    again: many thanks to everybody who replied and devoted me. many new insights were given and i developed -i think- a new coding style. i can't say this enough: thanks!

    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame
Re^2: The weirdest problem with undef
by Jasper (Chaplain) on Dec 23, 2004 at 09:54 UTC
    What if the subroutines had been exported from another package?
Re^2: The weirdest problem with undef
by blazar (Canon) on Jun 28, 2005 at 10:18 UTC
    In this case your best bet would be to declare my (@array1, @array2) before the for loop if you want them to be available to your subroutines.

    [SNIP]

    This clearly tells me that the subroutine is looking for global variables. Declaring them as my inside a loop doesn't create global variables. So moving them to the top, like this, works fine:

    OTOH this suggestion, in this form may contribute to suggest the OP to declare all of his own variables at the top of his scripts, which is indeed a bad habit many newbies have, and goes against proper scoping of variables.

    I do use stuff like

    { my $var; sub bus { $var++; } }
    myself, occasionally. But not as an alternative to parameter passing...