Pure Programmer
Blue Matrix


Cluster Map

Console Input

L1

This page is under construction. Please come back later.

ConsoleInput1.pl
#!/usr/bin/env perl
use utf8;
###############################################################################
# This program demonstrates how to prompt the user for input.
# 
# Copyright © 2020 Richard Lesh.  All rights reserved.
###############################################################################

use Utils;
use strict;
use warnings;

MAIN:
{
	binmode(STDOUT, ":utf8");
	binmode(STDERR, ":utf8");
	binmode(STDIN, ":utf8");
	my $name;
	$name = Utils::prompt("What is your name? ");
	my $favoriteColor;
	$favoriteColor = Utils::prompt("What is your favorite color? ");
	print Utils::messageFormat("Hello, \{0:s\}!  I like \{1:s\} too!", $name, $favoriteColor), "\n";
}

Output
$ perl ConsoleInput1.pl < ../../examples/ConsoleInput1.in What is your name? Rich What is your favorite color? blue Hello, Rich! I like blue too!
ConsoleInput2.pl
#!/usr/bin/env perl
use utf8;
###############################################################################
# This program demonstrates how to prompt the user for input.
# 
# Copyright © 2020 Richard Lesh.  All rights reserved.
###############################################################################

use Error::Simple;
use Nice::Try;
use Utils;
use strict;
use warnings;

# Try/Catch is added to Perl with cpan -i Syntax::Keyword::Try
MAIN:
{
	binmode(STDOUT, ":utf8");
	binmode(STDERR, ":utf8");
	binmode(STDIN, ":utf8");
	try {
		my $favoriteInt;
		my $favoriteLong;
		my $favoriteDouble;
		my $favoriteIntInput;
		$favoriteIntInput = Utils::prompt("What is your favorite small integer? ");
		$favoriteInt = Utils::stoi($favoriteIntInput);
		my $favoriteLongInput;
		$favoriteLongInput = Utils::prompt("What is your favorite large integer? ");
		$favoriteLong = Utils::stoi($favoriteLongInput);
		my $favoriteDoubleInput;
		$favoriteDoubleInput = Utils::prompt("What is your favorite floating point? ");
		$favoriteDouble = Utils::stod($favoriteDoubleInput);
		my $sum = $favoriteInt + $favoriteLong + $favoriteDouble;
		print Utils::messageFormat("All together they add up to \{0:f\}!", $sum), "\n";
	} catch (Error::NumberFormatException $ex) {
		print "Bad input! " . $ex, "\n";
	} catch (Error::Simple $ex) {
		print "Don't know what went wrong!\n";
	}
}

Output
$ perl ConsoleInput2.pl < ../../examples/ConsoleInput2.in1 What is your favorite small integer? 326 What is your favorite large integer? 1000000 What is your favorite floating point? 3.141926 All together they add up to 1000329.141593! This always prints! $ perl ConsoleInput2.pl < ../../examples/ConsoleInput2.in2 What is your favorite small integer? 326 What is your favorite large integer? abc What is your favorite floating point? 3.141926 Bad input! This always prints! $ perl ConsoleInput2.pl < ../../examples/ConsoleInput2.in3 What is your favorite small integer? 326 What is your favorite large integer? 1000000 What is your favorite floating point? abc Bad input! This always prints!

Questions

Projects

More ★'s indicate higher difficulty level.

References