in reply to Re^3: The weirdest problem with undef
in thread The weirdest problem with undef

dave, many thanks for this clarification... i obviously made a big mistake! i'll test it now...
--
to ask a question is a moment of shame
to remain ignorant is a lifelong shame

Replies are listed 'Best First'.
Re^5: The weirdest problem with undef
by insaniac (Friar) on Dec 22, 2004 at 10:32 UTC
    EVERYBODY: thanks for your replies, i changed the my (@source,@destination) into local (@source,@destination) and now it works like it should.

    so thanks again for this simple lesson..
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame

      See, that's exactly the quick hacky fix that I'd deliberately _not_ given you.

      Please do it the right way. Pass the lexical variables into the subroutines. There's rarely a good reason to use package variables.

      And please use "strict" and "warnings". It'll make it much easier to catch bugs like this in the future.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        hey dave,

        indeed, i know... it's not beautiful et al what i just did... but i needed a quick solution.

        and yes, i should use warnings and use strict (and usually i do), but this script is 3500+ lines and using strict on that script, phieeeuw, it's not good for the faint of heart. ;-) (it was written when i was a total perl newbie ;-) shit.. i still am!!! )

        but i'll try and update some of the subroutines to take the two variables as arguments instead of using the package variables...

        so thanks again for the hard lessons

        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame

      But this still isn't doing what you are expecting. Not declaring those two variables would produce the same result (given the amount of code you have given). A local block is making a local copy of those global variables for use inside the block, but you are altering and accessing both variables outside of the block... It may behave as you expect, but it isn't doing what you think it is.

      Update: When I said copy, I didn't mean the global values are retained. Wrong use of the word. I meant 'reset for use inside of the block.'

        A local block is making a local copy of those global variables in the block, but you are altering and accessing both variables outside of the block...

        Er... there is only every one copy of each package variable. "local" resets the value of a variable (whilst storing away the existing value for reuse later). Therefore it doesn't matter where you access the variable. Until you exit the block containing the "local" call, you will always get the "localised" version.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        ah ok.. i'll read some doc again then about the local thing, but i thought it was helping me..

        and eventually i rewrote the code into:

        my ($source, $destination) = get_elements ; scan_matrix $mode, $source, $destination;
        in the loop... where $source and $destination are references to arrays...

        what amazes me about perlmonks is: jeeeez, long after this problem is solved (for me), ppl keep experience-- me. so, it's not allowed to ask beginnersquestions without being degraded in your status (i mean: okay, i made a mistake... but does EVERYBODY need to degrade my status?)? hm...
        makes me think twice before i ask anything on this forum.
        /me crawls back in his cave...

        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame
      Ouch! While there are indeed (at least) Seven Useful Uses of local, IMHO there are tons of better WTDI.

      Honestly I can hardly imagine how you could devise such a byzantine scheme where parameter passing into the subs would be the most natural choice...