On Aug 19, Steve said:

>#!/usr/bin/perl

You should turn on warnings and use strict.

  #!/usr/bin/perl -w

  use strict;

If you're using Perl 5.6+, you can remove the -w and replace it with

  use warnings;

>print "What year were you born in?\n";
>$a = <STDIN>;
>chop($a);

  chomp(my $birth = <STDIN>);

>$b = system "date +%Y";

First, system() executes a command, it doesn't return its output.

  my $ok = system "date +%Y";  # prints the output of 'date +%Y'
                               # stores return status (0 == ok) in $ok

  chomp(my $curr_year = `date +%Y`);  # stores the output of 'date +%Y'
                                      # in $curr_year, and removes newline

Second, there's no need to spawn a program to get the year!

  my $curr_year = (localtime)[5] + 1900;  # 6th element returned is year
                                          # with 1900 subtracted from it

>$age = $b - $a

You're missing a semicolon here!

  my $age = $curr_year - $birth;

>print "You are $age years old!\n";

That's ok.

>when I execute this I get the following:
>
>What year were you born in?
>1986                   <~~~~~~I typed this then pushed enter
>2002
>You are -1986 years old!

You'll notice '2002' was printed to the screen.  Why?  Because system()
merely executes the command (which, in 'date's, case, prints some
information to the terminal).  It RETURNS 0 (because 'date' executed
successfully), and 0 - 1986 = -1986.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to