On Aug 6, 6:52 am, [EMAIL PROTECTED] (Amichai Teumim) wrote:
> This is pure educational. I want to understand how this all works.
>
> So after follow your comments my script.pl looks like this:
>
> !/usr/bin/perl

This shebang is incorrect.  Specifically, you're missing the "sh" part
of "shebang":

#!/usr/bin/perl

> use strict;
> use warnings;
>
> require 'lib.pl';
>
> my @userArray = <STDIN>;
>
> my $sum = sumIt(@userArray);
>
> print $sum;
>
> AND my library like this:
>
> sub sumIt(

A parenthesis does not begin a subroutine.  A curly-brace does.

>  my $total;
>  $total += $_ for @_;
>  warn "@_ was empty, total undefined!\n" if !defined $total;
> }

You misread my post.  I said this is what you very specifically DO NOT
want to do.  I said this sort of thing is exactly why you should
ALWAYS use an explicit return statement.

sub sumIt {
   my $total;
   $total += $_ for @_;
   warn "[EMAIL PROTECTED] was empty, total undefined!\n" if !defined $total;
   return $total;
}

>
> sub avg(@)

you are still using prototypes, after being told why not to.

> {
>  my @arr = @_;
>  my $arrSize = scalar(@arr);

Now that you've elimintated the $arrSize variable from the division
below, there is no reason to create it in the first place.

> (last index). Double check

This is a syntax error, as you haven't preceded your comment with a #
mark

>  return sumIt(@arr) / @arr;
>
> }
>
> 1;
>
> Is this correct now?

No.  See above.

> If so, I don't know how to use it. What command should
> I use to try this out?

Is this your first Perl script ever?  If so, you really need to be
reading
perldoc perlintro
long before worrying about subroutines and prototypes and return
values and creating libraries.

If not, you run it the same way you run any other Perl script.
Either
perl script.pl
or make script.pl executable:
chmod u+x script.pl
and then run it:
./script.pl

> P.S. echo 1234 | ./script.pl doesn't do anythign exciting.

Yes it does.  It gives you all the syntax errors you had in your code.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to