Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: calculator help

by Laurent_R (Canon)
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]
[erix]: go to the bottom of this page: Seekers of Perl Wisdom
[karlgoethebier]: thao4: Si tout le reste échoue demande en francais ;-) There are some French speaking monks in the monastry...
[erix]: weird: a dev version perl (perl-5.27.9) is released, but git doesn't contain a perl-5.27.10 yet
[karlgoethebier]: ...not me ;-)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2018-02-21 10:04 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (276 votes). Check out past polls.