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

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

Replies are listed 'Best First'.
Re^6: The weirdest problem with undef
by davorg (Chancellor) on Dec 22, 2004 at 10:40 UTC

    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
        i needed a quick solution
        This weird bug with using package variables made it not so quick eh? Beware of false laziness and false quickness.
Re^6: The weirdest problem with undef
by perlguy (Deacon) on Dec 22, 2004 at 14:31 UTC

    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

        You're getting downvoted because you made a quick hacky fix. You knew it was a quick hacky fix when you made it but you still went ahead. And you implied that you thought it would be too much effort to fix your script to work under "strict" and "warnings".

        Take a look at the Pragmatic Programmers' list of tips. Right near the top you'll see "don't live with broken windows". You sounded like you were happy to live with broken windows and a lot of people round here disapproved of that.

        So you're not getting downvoted because you asked a beginner question. You're getting downvoted because you strongly implied that you're not a beginner but you weren't prepared to make the correct fix. It sounds like you've now made a start on doing the right thing so you can expect to get some more positive votes.

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

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

        It is much more important to learn around here than to gain experience. I would continue to post. You'll be glad you did in the long run.
Re^6: The weirdest problem with undef
by blazar (Canon) on Jun 28, 2005 at 10:08 UTC
    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...