[prog] speaking Perlish

Meryll Larkin alwanza at oz.net
Fri Oct 25 17:37:53 EST 2002


10/25/02

beginning Perl code for Kenny

code:

#!/usr/bin/perl -w      # love that She-bang!

#  Hey, Kenny.  It's a good start!  Perl is fun!

use strict;  # this is considered a good programming practice.
#  start doing it now and it will save you lots of headaches in
#  the future.  It forces you to declare the scope of all your
#  variables.  This probably doesn't mean much to you now, but
#  it will soon.

# Allow user to respond on same line - so use no new line
print "How many hours do you sleep everyday?  ";

chomp (my $sleep =3D <stdin>);
my $hours =3D 365*$sleep;

#  note over here you could be getting a long "float"
#  decimal number.  Is that what you want?

my $days =3D $hours/24;
my $workingdays =3D 365 - $days;
print "\nWow! you sleep $hours hours per year,\n";
print "    which equals $days days per year\n";
print "You have slept $workingdays working days each year!!!\n";

#  The next output needs to be fixed
#  print "\nAnd how long do you expect to live? ";
#  This question invites the user to write a number and then
#  the word "years" after it.  But you haven't done any data
#  validation and are clearly doing only numerical calculation

print "\nAnd how many years do you expect to live? ";
chomp (my $live =3D <stdin>);

# data validation using Regex
$live =3D~ s/\D*//g;  # strip off all non-digits

if (!$live) {  # make sure there is a number in there.
    print "Please answer numerically.  Quitting.\n";
    exit 1;
}

my $sleepyears =3D $live*$days;
my $x =3D $sleepyears/365;
print "You'll be sleeping $x years of your entire life-time.\n";

exit 0;


-- END of code --

Meryll (I enjoy Programming Perl) Larkin

--=20
Meryll Larkin
http://www.alwanza.com/poems/programmingPerl.html
Perl Programmer =96 Web Developer - Linux Apache Admin
Seattle, WA, USA



> From: "kansas_kennedy at phreaker.net" <kansas_kennedy at phreaker.net>
> To: programming at linuxchix.org
> Date: Fri, 25 Oct 2002 03:47:50 -0400
> Subject: [prog] Perlish
>=20
>=20
> --------------Boundary-00=3D_QN1JK7CYS7TV8B5GVLDG
> Content-Type: text/plain;
>   charset=3D"us-ascii"
> Content-Transfer-Encoding: quoted-printable
>=20
> Hi group,
>=20
> I am new to Perl. I have written this program which basically deals wit=
h =3D
> your=3D20
> age, how many years you'd be sleeping in your entire life-time etc. etc=
=2E
>=20
> The code is given below and the program is attached.
>=20
> Please review the code and let me know if it lacks anything.
>=20
> JAPH,
> Kenny.
>=20
> Code:
> ~~~~~~
> #!/usr/bin/perl -w
> print "How many hours do you sleep everyday\n";
> chomp ($sleep =3D3D <stdin>);
> $hours =3D3D 365*$sleep;
> $days =3D3D $hours/24;
> $workingdays =3D3D 365 - $days;
> print "Wow! you sleep $hours hours a year, which equals to $days days a=
 y=3D
> ear=3D20
> in total\n";
> print "You have $workingdays workingdays in one year!!!\n";
> print "And how long do you expect to live?\n";
> chomp ($live =3D3D <stdin>);
> $sleepyears =3D3D $live*$days;
> $x =3D3D $sleepyears/365;
> print "you'd be sleeping $x years of your entire life-time\n";
>=20
> --------------Boundary-00=3D_QN1JK7CYS7TV8B5GVLDG
> Content-Type: text/x-perl;
>   charset=3D"us-ascii";
>   name=3D"sleep.pl"
> Content-Transfer-Encoding: 7bit
> Content-Disposition: attachment; filename=3D"sleep.pl"
>=20
> #!/usr/bin/perl -w
> print "How many hours do you sleep everyday\n";
> chomp ($sleep =3D <stdin>);
> $hours =3D 365*$sleep;
> $days =3D $hours/24;
> $workingdays =3D 365 - $days;
> print "Wow! you sleep $hours hours a year, which equals to $days days a=
 year in total\n";
> print "You have $workingdays workingdays in one year!!!\n";
> print "And how long do you expect to live?\n";
> chomp ($live =3D <stdin>);
> $sleepyears =3D $live*$days;
> $x =3D $sleepyears/365;
> print "you'd be sleeping $x years of your entire life-time\n";
>=20






More information about the Programming mailing list