Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: calculator help

by Laurent_R (Abbot)
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.

Replies are listed 'Best First'.
Re^2: calculator help
by GrandFather (Sage) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1088213]
[Your Mother]: A shot of espresso, while concentrated, is only 1.5 ounces. Most coffees are served at at least 10 oz. So a cup of coffee has quite a bit more caffeine.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-01-16 10:48 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (148 votes). Check out past polls.