Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: calculator help

by Laurent_R (Parson)
on Jun 01, 2014 at 22:35 UTC ( #1088213=note: print w/ replies, xml ) Need Help??


in reply to calculator help

Lady_Aleena is (almost completely ) right: at the very least, anything that you get from the user using <STDIN> should be chomped to get rid of the end of line characters (the only very minor point where she is not completely right is that she has a small typo on the name of the variable, $opt1 instead of $opr1).

This is especially important for the operation ($opr1 variable), that will never be equal to "+" (for example) if you don't chomp it, but more probably to things like "+\n" (under Unix) or "+\r\n" (under Windows) or something else (on Mac, I don't know for sure, it used to be "+\r", but I think it has changed some times ago and is probably like Unix nowadays). Anyway, whatever the trailing character(s) is not so important, the point is that you should remove it using the chomp function.


Comment on Re: calculator help
Download Code
Re^2: calculator help
by GrandFather (Cardinal) on Jun 03, 2014 at 10:28 UTC

    Actually \n on all operating systems unless you do some work to make ity otherwise. Perl translates "local" line end conventions to \n during file I/O so you don't usually have to worry about it.

    Perl is the programming world's equivalent of English
      Thank you for the information, GrandFather, I did not know a conversion was done during I/O, I thought that it was chomp that was clever enough to remove "\n" or "\r\n" depending on whether the script was running on Unix or Windows, for example.

      We regularly have issues at work in a different type of situation, when we process files generated by another application running on another system, with end of lines sometimes converted and sometimes not converted during the transfer (FTP in ASCII or bin mode, SFTP, scp, etc.). We then need to pre-process the file before feeding them to our programs (in some cases we even have changed our programs so that they can handle smoothly both cases).

        See PerlIO.

        Perl is the programming world's equivalent of English

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-12-17 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (31 votes), past polls