4 Replies - 7277 Views - Last Post: 22 October 2012 - 01:11 AM

#1 W4tchM4ker  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 34
  • Joined: 18-October 12

Question on simple calculator

Posted 18 October 2012 - 12:43 PM

Hey everyone. I'm fairly new to programming and the dream.in.code community. That being said, I decided to pick up perl as my first programming language. It was recommended to me for its ease of use and the fact that you can read it easily. Now, I wanted to create a very simple calculator program. I was able to do that, but I wanted to be able to type the word "finished" instead of hitting ctrl+z to end the program (no particular reason, really, I just wanted to experiment with the language. That's how you learn, right?). The problem is that I get an error saying "use of uninitialized value in print at line 11, <STDIN> line 1." I think I'm really missing something basic and just not paying attention. I'd appreciate any explanation and guidance. Anyway, here's the code:

#!/usr/bin/perl
#calc.pl
use warnings;
use strict;

my $finished = 0;

print "Welcome!\n";                           
print "Type 'finished', to exit.\n";
$|++; print "> ";
print eval, "\n> "
while chomp( $_ = <STDIN> );

$finished = <STDIN>;
die;



Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Question on simple calculator

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Question on simple calculator

Posted 18 October 2012 - 12:50 PM

** Topic moved to Perl & renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Question on simple calculator

Posted 19 October 2012 - 02:42 AM

View PostW4tchM4ker, on 18 October 2012 - 08:43 PM, said:

The problem is that I get an error saying "use of uninitialized value in print at line 11, <STDIN> line 1."

print eval, "\n> "



What do you expect that line to do?

What it does do is attempt to evaluate an empty line of Perl code (which is what the eval command does), print the value returned by eval (which, because it has no code to evaluate, returns an undefined value), and then print "\n> ". The warning comes because you're printing the undef returned by eval, but what I don't get is why the eval is there in the first place, when you're not giving it any code to evaluate.

Kudos for using strict and warnings right from the start, by the way!
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,274
  • Joined: 21-June 11

Re: Question on simple calculator

Posted 19 October 2012 - 03:18 AM

View Postdsherohman, on 19 October 2012 - 11:42 AM, said:

View PostW4tchM4ker, on 18 October 2012 - 08:43 PM, said:

print eval, "\n> "



What do you expect that line to do?


He's expecting it to evaluate each line read by the while-loop, which it does (note that there's no semicolon there, so the while-loop on the next line is still part of the same statement). The warning doesn't appear until the user closes the stream. Before that everything works fine.

What I don't get is what the $finished = <STDIN>; bit at the end is supposed to accomplish.
Was This Post Helpful? 1
  • +
  • -

#5 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Question on simple calculator

Posted 22 October 2012 - 01:11 AM

View Postsepp2k, on 19 October 2012 - 11:18 AM, said:

View Postdsherohman, on 19 October 2012 - 11:42 AM, said:

View PostW4tchM4ker, on 18 October 2012 - 08:43 PM, said:

print eval, "\n> "



What do you expect that line to do?


He's expecting it to evaluate each line read by the while-loop, which it does (note that there's no semicolon there, so the while-loop on the next line is still part of the same statement).


Ah, yes, you're right... I had missed that there wasn't a semicolon. (Note to the OP: When you use a construct like that, you should probably either put it all on one line or indent the "while" so that it's more visible that the two parts are related.)

View Postsepp2k, on 19 October 2012 - 11:18 AM, said:

The warning doesn't appear until the user closes the stream. Before that everything works fine.


Now that I actually try the code, I get a different warning than the OP - I get "Use of uninitialized value $_ in chomp" rather than a complaint about printing an undef value. This makes perfect sense, given that I'm ending the file with ctrl-D, which causes <STDIN> to return undef, so there you go: "use of uninintialized value in chomp".

The OP's different error message (assuming it's accurate) may be due to running under Windows - he mentioned usting ctrl-Z to exit, which would suspend the program rather than closing its STDIN if he were in an sh/bash-type shell. Plus I have a vague memory of Windows using ctrl-Z as an EOF marker.

In any case, W4tchM4ker, to get this to work the way you want (allowing the user to exit by entering "finished"), as well as to fix the warnings on exit, you'll need to use a "while { block }" construct instead of using the while as a statement modifier so that you can run multiple commands in the body of the while. This will allow you to inspect the value of the user's input before trying to execute it. Something like:
while (<STDIN>) {
  chomp;
  exit if lc($_) eq 'finished';
  print eval, "\n> ";
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1