in reply to Re^2: Make select apply to modules as well
in thread Make select apply to modules as well

Please write a small sample of code that you think should work yet doesn't. If the situation is as you describe, then I'll wind up suggesting that you use the perlbug utility to report a bug in Perl.

However I strongly suspect that you're missing something, and select works as it is documented. Perhaps select is called again, perhaps the messages you're complaining about come on STDERR thanks to warnings, perhaps a million things.

But the vanishingly unlikely scenario is that you're encountering a bug in Perl. I don't mean to suggest that you're lying - we've all had the experience of being sure that Perl has a bug because we don't see how what we are seeing is possible otherwise. But, even still, this experience is almost always followed by a, "D'oh!"

  • Comment on Re^3: Make select apply to modules as well

Replies are listed 'Best First'.
Re^4: Make select apply to modules as well
by richz (Beadle) on Dec 05, 2004 at 21:52 UTC
    Ok, what is happening is that the default filehandle seems to be changed inside my module as it should UNTIL I make a call to another subroutine which does an in-place edit of a file.

    Does changing $^I(which I do for the in-place edit) screw anything up?

    Thanks.

      I believe that you have correctly identified the problem.

      This is documented, but obscurely. If you check in perlvar you'll find that $^I is the current value of the in-place edit. If you check in perlrun you'll find that -i calls select.

      I would suggest avoiding $^I and writing the code that it saves for you. Alternately call select before setting $^I to get the currently selected filehandle, and then call select when the edit is done so that $^I and select play nice with each other.

        Thanks bro. After looking some more I realized it probably does affect something because when doing the in-place edit a print without specifying a filehandle prints to the file. I am just going to save the filehandle before I do the in-place edit and then restore it.

        Thanks again.

      Glad you found the problem. Consider using Tie::File as a practical alternative if you're doing in-place editing. (In some cases it may not be appropriate -- read the docs -- but for many cases, it is.)
Re^4: Make select apply to modules as well
by richz (Beadle) on Dec 05, 2004 at 20:52 UTC
    Tilly,

    I certainly don't mean to say there is a bug in Perl. I am sure I am doing something wrong but it is not at all obvious to me. I will do some more debug of my own and let you know. It is hard for me to post the code because it is a lot of code and I'd prefer not to do so unless I can't figure it out.

      You might not mean to say that there is a bug in Perl, but you are saying it!

      What you're asking boils down to, "I want Perl to work as documented." Well Perl does. So the problem isn't there - it is somewhere else in your code.

      As for figuring your code out, I strongly advise that you get in the habit of writing little snippets to test theories like this. Because right now you're staring at a mass of code and you are coming up with a theory. You aren't testing that theory, or verifying it, you are coming up with it and then posting for help based on your theory. But your theory is wrong. Nobody can provide you with help based on your theory, because you haven't actually identified your problem!

      This isn't an effective debugging strategy. It is a very common one that people naturally fall into. It takes a lot of experience and patience to avoid this trap (been there, done that - we all have). And you won't progress until you start doing something more effective.

      So sitting where I am, all that I can do is tell you that the problem isn't what you think it is. You'll have to look elsewhere. I told you a couple of other places to look. But without your code I can't do more than guess either. (Again, not very effective. But at least my guesses are based on a lot of experience about what can go wrong, so I'll come up with theories that are possible, if not likely.)

        I've done plenty of debugging bro, but I'm a C/C++/Java programmer and a realtively new Perl one. Therefore, my first intent was to just figure out whether select should have the effect I wanted it to have. Secondly, I didn't want to waste my time debugging the problem if I found out later that that is the expected behavior of select.

        Anyways, as it turns out it is some strangeness as I explained above in another post. Perhaps something to do with modifying $^I.

        Thanks for your help, have a beer. Cheers!