Perl and Terminal w/OS 10.5.5

I am studying Perl as a total newbie and trying to write a simple Perl program to ask for User Input. However, I don't know how to test that in Terminal - where does the User input his or her name, for instance? I wrote this code, but don't know if it's close to right, as I don't know how to test it since it asks for user variables. Any help would be much appreciated.

'print "What is your last name?\n";
$lastname = <STDIN>;
print "What is your first name?\n";
$firstname = <STDIN>;
print "Please enter three single digits numbers separated by commas.\n";
@numbers= <STDIN>;
print "You entered '@numbers'.";
$firstinitial=index("$firstname", 0, 1);
print "Your user name is '$lastname$firstinitial'.\n";
print "Your password is ('@numbers', 2, 1, 0)";'

Thanks,
Karen

MacBookPro 15", Mac OS X (10.5.5)

Posted on Oct 27, 2008 11:57 PM

Reply
4 replies

Oct 28, 2008 6:17 AM in response to kznadalin

You test it by running it! First, turn it into a proper Perl script with a shebang line. The shebang isn't really required, but can be useful so that you can run the script without having to type "perl" first.


#!/usr/bin/perl -w
print "What is your last name? ";
$lastname = <STDIN>;
print "What is your first name? ";
$firstname = <STDIN>;
print "Please enter three single digits numbers separated by commas. ";
@numbers= <STDIN>;
print "You entered '@numbers'.";
$firstinitial=index("$firstname", 0, 1);
print "Your user name is '$lastname$firstinitial'. ";
print "Your password is ('@numbers', 2, 1, 0)";

save it is input.pl or something similar.

You can run it by typing "perl input.pl". If you want to get fancy, you can do

chmod +x input.pl

and then run it by just typing "input.pl".

Still, it isn't going to work properly due to:

@numbers= <STDIN>;

When you read from standard input in list context, you read until end-of-file. First, read it into scalar like:

$numbers=<STDIN>;

Then, split it into an array like so:

@numbers=split /,/, $numbers;

Also, don't forget to "use strict" and "chomp" your input. Here is the final version:

#!/usr/bin/perl -w
use strict;
print "What is your last name? ";
my $lastname = <STDIN>;
chomp $lastname;
print "What is your first name? ";
my $firstname = <STDIN>;
chomp $firstname;
print "Please enter three single digits numbers separated by commas. ";
my $numbers= <STDIN>;
chomp $numbers;
my @numbers = split /,/, $numbers;
print "You entered '@numbers'.";
my $firstinitial=index("$firstname", 0, 1);
print "Your user name is '$lastname$firstinitial'. ";
print "Your password is ('@numbers', 2, 1, 0)";

Oct 28, 2008 6:19 AM in response to kznadalin


#!/usr/bin/env perl
print "What is your last name? ";
$lastname = <STDIN>;
chomp($lastname); # remove
print "What is your first name? ";
$firstname = <STDIN>;
chomp($firstname); # remove
print "Please enter three single digits numbers separated by commas. ";
#@numbers= <STDIN>; # Perl doesn't know you want to split on commas
$numstr= <STDIN>; # I put 1,2,3 into a string
chomp($numstr); # remove
@numbers = split(/,/,$numstr); # now I split into an array on the commas
print "You entered '@numbers'.";
#$firstinitial=index("$firstname", 0, 1); # was giving -1
$firstname =~ /^(.)/; # so I used a different trick
$firstinitial = $1; # to get first letter
print "Your user name is '$lastname$firstinitial'. ";
print "Your password is ('@numbers', 2, 1, 0)";

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Perl and Terminal w/OS 10.5.5

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.