Clear questions and runnable code
get the best and fastest answer
Re: float: Perl is replacing dot with comma (automagic locale?)by muba (Priest)
|on May 15, 2014 at 15:14 UTC||Need Help??|
I don't know about the locale stuff, but I do know why your don't get the version number. Bare with me as I try to pull you step by step.
Compile time and run time
Running a Perl script consists of two phases: a compile phase, in which Perl does things like load modukles, import stuff from them, runs BEGIN blocks, and a whole lot of other things and eventually translates your code to an internal byte code format; and a run phase, in which the generated byte code is actually executed.
When running the code, it pretty much starts at the top of your script and moves down line by line. There are a few exceptions though, because in the compile phase it already collected information about your packages and subroutines and stuff. So if you call My::Test::Print::printer() near the top of your script, perl already knows where the body of this subroutine is because it saw it in the compile phase even though it didn't see it in the run phase yet.
Debugging, step 1: use strict; use warnings;
So what's going on here?
First of all, let's use strict and warnings, just to see if that's going to give us any insight into why perl things it should print the empty string for $My::Test::VERSION.
"Use of uninitialized value"? On one hand, this was kind of the warning that I was expecting since it wasn't printing the version number, but on the other hand it had me confused, because I can see the variable initialization just a few lines above!
So I stopped and did a classic run of eyeball debugging. What's that, you ask? Well, you start to play the role of the perl run time phase. Grab a sheet of paper and a pen - you'll be using this to keep track of variables and their values. Basically this sheet of paper is your very own symbol table. Awesome. At the top of this paper, write "SYMBOL TABLE:".
(Edit: (tl;dr: it's not really a symbol table, but I just call it that in this post. Sorry.) Lexical variables (that is, variables declared with my) aren't kept in the symbol table. I was aware of this, but at the time I chose for a simplification of facts and chose to just roll with that term. However, it has been brought to my attention that this simplification may be misleading, for which I apologize.)
Now start at the top of your script, and mentally run the code: look at each line, determine what you think the line does, and if necessary, write down any variable that's created and any value that is assigned, changed, undef'ed, or whatever.
Mentally run that line and write the appropriate stuff. So you'll have this:
Still with me? Awesome. Next line.
Look at your symbol table sheet of paper - see if you have the $test variable there. Do you? What value does it have? What does get printed (not in your mental execution of this code, but what does perl actually print?) Does that match what you have on your symbol table?
Ignoring the decimal comma vs. decimal period (because I don't know why that happens), yes, it does. Wonderful. Let's go on.
To determine what that will print, we'll have to jump to that sub and mentally run that. Let's go.
Look at your symbol table paper again. See if you have $My::Test::VERSION in there. Do you?
No, you don't. The run time phase hasn't been there yet.
"But if I put my package in its own file and then use it..."
How come it suddenly starts working when I move stuff into its own file? Remember what I said near the beginning? In the compile phase, perl loads modules - so by putting MyTest into its own module I've made sure that $MyTest::VERSION gets set before the bulk of my script is executed!
But I don't want to move stuff into separate files!
That's fine, too, but you'll have to tell Perl somehow that it should set the VERSION number before it does anything else. So use a BEGIN block.
Hope this helps.