Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: The error says the value is uninitialized, but it works anyway

by hippo (Chancellor)
on Aug 20, 2019 at 09:15 UTC ( #11104717=note: print w/replies, xml ) Need Help??


in reply to Re^3: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

In simple terms, every Perl variable starts out as a string.

I have never heard this before. Do you have a source for this, please?

  • Comment on Re^4: The error says the value is uninitialized, but it works anyway

Replies are listed 'Best First'.
Re^5: The error says the value is uninitialized, but it works anyway
by Marshall (Abbot) on Aug 20, 2019 at 19:59 UTC
    Well "simple terms" were perhaps too simple. This is not true if you have a program statement say $x=32; In the context of the question being asked (inputing a value from the console), this is true and I discussed some of the ramifications of that and gave demo code. In the subroutine that prompts for the float... In version #1, I just strip off the leading and trailing spaces with a regex. The returned value is a string. However, as I point out an alternate way to "strip off the white space" (sort of) is to force Perl to convert the string to a numeric value. You can do this by simply adding 0 to it. $float+=0; When you do this to an int value, it is pretty much like stripping the whitespace. However with a float, there will be usual storage imprecision and possible extension digits after the decimal. 6.3 might be 6.33333333, etc. As I point out, if you had inputted say "+5.0", if you use the "add 0" method, the plus sign will not be there when printed out in the calling routine because Perl will be using the numeric value, not the string value we started with. Of course when inputting a string and doing math upon it, you have to ensure the string is a valid number, else there will be at least some kind of warning message generated!

    The important point for the OP which perhaps got lost, is that you don't have to call any special function to cause the conversion to a numeric value - in some languages you do, but not in Perl. (In C you could use a %f format spec for the read and there is no string value, you get a binary number straight away). In Perl there will be a string value and you just do math on that string and Perl will do "the right thing".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104717]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2019-09-16 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (185 votes). Check out past polls.

    Notices?