Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

library (random numbers)

6 views
Skip to first unread message

Amichai Teumim

unread,
Aug 3, 2007, 6:03:21 AM8/3/07
to begi...@perl.org
After some friendly input from yitzle I might have moved further with
my library.

This is my script.

script.pl


#!/usr/bin/perl

#require 'lib.pl';

@userArray = <STDIN>;

$sum = sumIt(@userArray);

print $sum;

And this is my library according to yitzle:


sub sumIt(@)
{
my $total = 0;
$total += $_ for (@_);
return $total; # This line might not be needed...
}

sub avg(@)
{
my @arr = @_;
my $arrSize = @arr; # scalar(@arr) is the array size - or one less
(last index). Double check
return simIt(@arr) / $arrSize;
}

1;

Now either this is wrong or I have no idea how to use it.

With STDIN I need to do something like this (right?):

./script.pl | echo 1234

Or is this nonsensical? Very very new to Perl.

Thanks

Amichai

Mr. Shawn H. Corey

unread,
Aug 3, 2007, 9:38:30 AM8/3/07
to Amichai Teumim, begi...@perl.org
Amichai Teumim wrote:
> ./script.pl | echo 1234
>
> Or is this nonsensical? Very very new to Perl.

This is actually shell:

echo 1234 | ./script.pl

Try:

./script.pl <<EOD
1234
5678
90
EOD


--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

Chas Owens

unread,
Aug 3, 2007, 10:33:33 AM8/3/07
to Amichai Teumim, begi...@perl.org
On 8/3/07, Amichai Teumim <amichai...@idtglobal.com> wrote:
snip
> sub sumIt(@)
snip
> sub avg(@)
snip

Ack prototypes! They don't do what you think they do. Don't use them
until you have read
http://library.n0i.net/programming/perl/articles/fm_prototypes and
understand it fully. Prototypes are very useful in specific areas and
harmful or a waste of your time in all others. Perl 6 is adding
formal parameters (which is what people mistake prototypes for), so
this will not be a problem in it.

Paul Lalli

unread,
Aug 3, 2007, 11:04:24 AM8/3/07
to begi...@perl.org
On Aug 3, 6:03 am, amichai.teu...@idtglobal.com (Amichai Teumim)
wrote:

> After some friendly input from yitzle I might have moved further with
> my library.
>
> This is my script.
>
> script.pl
>
> #!/usr/bin/perl

Get into the habbit of using
use strict;
use warnings;
in all of your scripts. You will be greatful you did in the long
run. With such small libraries/scripts as these, it's not as
important as mistakes are far easier to debug, but getting into the
habbit now will save you endless amounts of pain later on in your
programming life.


> #require 'lib.pl';
>
> @userArray = <STDIN>;

my @userArray = <STDIN>;
because 'use strict;' forces you to declare your variables.

> $sum = sumIt(@userArray);

my $sum = sumIt(@userArray);

Out of curiousity, is this a learning excercise, or do you intend to
actually use this? If the latter, you should know that this wheel has
already been invented. The standard List::Util module provides a
sum() function:

use List::Util qw/sum/;
my $sum = sum(@userArray);

> print $sum;
>
> And this is my library according to yitzle:
>
> sub sumIt(@)

1) Don't use prototypes. They don't work like anyone expects them to.
2) This particular prototype is double-plus useless. It says that
this function takes a list of values. That's what all subroutines
without prototypes take. That (@) is doing nothing at all.

> {
> my $total = 0;
> $total += $_ for (@_);
> return $total; # This line might not be needed...

Not needed, but again a good habbit to get into. Never rely on the
"blocks return the last evaluated value" feature of Perl. Return
explicitly, so that you don't FUBAR things when you later go back to
modify your code. For example, if you had just written:

sub sumIt {
my $total;
$total += $_ for @_;
}

and then later you wanted to add a warning if there hadn't been
anything in @_:

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

Now you've FUBARed your script. The last value evaluated will either
be the (!defined $total), or the (warn "..."). It won't be $total.
If you'd started with the explicit return statement, you'd save
yourself this problem.

>
> }
>
> sub avg(@)
> {
> my @arr = @_;
> my $arrSize = @arr; # scalar(@arr) is the array size - or one less
> (last index). Double check

scalar(@arr) is the size of the array
$#arr is the last index of the array.

USUALLY it is a true statement that scalar(@arr) == $#arr + 1;
However, this is not necessarily the case, as in Perl you can actually
futz with the starting index of arrays using the $[ variable. You
should never do that, of course, but you also shouldn't assume that no
one in your program has.

> return simIt(@arr) / $arrSize;

Well first, assuming you meant sumIt, not simit, there's no reason to
create a new variable just to store the size of the array. Just use
the array in a scalar context:

return sumIt(@arr) / @arr;

>
> }
>
> 1;
>
> Now either this is wrong or I have no idea how to use it.
>
> With STDIN I need to do something like this (right?):
>
> ./script.pl | echo 1234

That says you want to run script.pl and send the output of script.pl
to the process "echo 1234". You actually want the other way around:

echo 1234 | ./script.pl

> Or is this nonsensical? Very very new to Perl.

Well, this bit in any event has nothing to do with Perl. Input/Output
redirection is a feature of the shell, not of Perl.

Paul Lalli

Mr. Shawn H. Corey

unread,
Aug 6, 2007, 5:57:13 AM8/6/07
to Amichai Teumim, begi...@perl.org
Amichai Teumim wrote:
> When I do this, I get :

>
> ./script.pl <<EOD
> 1234
> 5678
> 90
> EOD
>
> Undefined subroutine &main::sumIt called at ./script.pl line 7, <STDIN>
> line 3.
>
> #!/usr/bin/perl
>
> #require 'script.pl';

require 'lib.pl';

>
> @userArray = <STDIN>;
>
> $sum = sumIt(@userArray);
>
> print $sum;

If the sub sumIt() is in the file lib.pl, you have to tell perl where to find it.

Amichai Teumim

unread,
Aug 6, 2007, 6:52:09 AM8/6/07
to Paul Lalli, begi...@perl.org
Hi Paul

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

use strict;
use warnings;

require 'lib.pl';

my @userArray = <STDIN>;

my $sum = sumIt(@userArray);

print $sum;

AND my library like this:

sub sumIt(


my $total;
$total += $_ for @_;

warn "@_ was empty, total undefined!\n" if !defined $total;
}

sub avg(@)
{
my @arr = @_;

my $arrSize = scalar(@arr);
(last index). Double check
return sumIt(@arr) / @arr;
}

1;

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

Thanks

Amichai

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

> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>

Amichai Teumim

unread,
Aug 6, 2007, 7:56:10 AM8/6/07
to Mr. Shawn H. Corey, begi...@perl.org
I thought that

require 'lib.pl';

was telling Perl where to find it.

Paul Lalli

unread,
Aug 6, 2007, 8:49:08 AM8/6/07
to begi...@perl.org
On Aug 6, 6:52 am, amic...@teumim.com (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 "\@_ 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

Paul Lalli

unread,
Aug 6, 2007, 8:51:14 AM8/6/07
to begi...@perl.org
On Aug 6, 7:56 am, amichai.teu...@idtglobal.com (Amichai Teumim)
wrote:

> Mr. Shawn H. Corey wrote:
> > Amichai Teumim wrote:
> >> #!/usr/bin/perl
> >> #require 'lib.pl';

> > require 'lib.pl';

> >> @userArray = <STDIN>;
> >> $sum = sumIt(@userArray);
> >> print $sum;
>
> > If the sub sumIt() is in the file lib.pl, you have to tell perl where
> > to find it.

> I thought that


> require 'lib.pl';
> was telling Perl where to find it.

Yes, it would have, if that's what you had. But you didn't. You had
the line:
#require 'lib.pl'

That is, you had the line commented out. A commented out line does
nothing at all.

Paul Lalli

Mr. Shawn H. Corey

unread,
Aug 6, 2007, 9:03:38 AM8/6/07
to Paul Lalli, begi...@perl.org
Paul Lalli wrote:
> Yes, it would have, if that's what you had. But you didn't. You had
> the line:
> #require 'lib.pl'
>
> That is, you had the line commented out. A commented out line does
> nothing at all.
>
> Paul Lalli
>
>

Actually, in the post I replied to it was: #require 'script.pl';

The OP seems to think that we are keeping a copy of his program and whenever he changes something, our copy get automatically updated. Sorry, computers just don't work that way.

Amichai Teumim

unread,
Aug 6, 2007, 3:34:17 AM8/6/07
to Mr. Shawn H. Corey, begi...@perl.org
When I do this, I get :

./script.pl <<EOD
1234
5678
90
EOD

Undefined subroutine &main::sumIt called at ./script.pl line 7, <STDIN>
line 3.

#!/usr/bin/perl

#require 'script.pl';

@userArray = <STDIN>;

$sum = sumIt(@userArray);

print $sum;

Paul Lalli

unread,
Aug 6, 2007, 9:46:00 AM8/6/07
to begi...@perl.org
On Aug 6, 9:03 am, shawnhco...@magma.ca (Mr. Shawn H. Corey) wrote:
> Paul Lalli wrote:
> > Yes, it would have, if that's what you had. But you didn't. You had
> > the line:
> > #require 'lib.pl'
>
> > That is, you had the line commented out. A commented out line does
> > nothing at all.
>
> > Paul Lalli
>
> Actually, in the post I replied to it was: #require 'script.pl';

No, in the post you replied to, it was 'lib.pl':
http://groups.google.com/group/perl.beginners/msg/61455df84cce63c9

In your reply to said post, you changed it to look like he had
'script.pl':
http://groups.google.com/group/perl.beginners/msg/0bd8e1be8c6b0cdf

I changed it back in my followup.

Paul Lalli

Mr. Shawn H. Corey

unread,
Aug 6, 2007, 9:39:17 AM8/6/07
to Amichai Teumim, begi...@perl.org
Amichai Teumim wrote:
> No it was commented out. Now it's changed. I still don't know how to use
> it. What would a good way to test it be?
>
> echo 1234 | ./script.pl obviously won't do much. I wad told
> ./obj13-1.pl <<EOD
>

What the heck is obj13-1.pl?

To test your script you need a list of numbers one per line. The easiest way is:

$ perl script.pl
123
456
789
^D

The '^D' is generated by holding the Control (sometimes labeled Ctrl) key and pressing the d key at the same time.

Paul Lalli

unread,
Aug 6, 2007, 9:55:15 AM8/6/07
to begi...@perl.org
On Aug 6, 9:03 am, shawnhco...@magma.ca (Mr. Shawn H. Corey) wrote:
> Paul Lalli wrote:
> > Yes, it would have, if that's what you had. But you didn't. You had
> > the line:
> > #require 'lib.pl'
>
> > That is, you had the line commented out. A commented out line does
> > nothing at all.
>
> > Paul Lalli
>
> Actually, in the post I replied to it was: #require 'script.pl';

Okay. I'm an ass. Google Groups is having (yet more) problems today,
and one of them resulted in me seeing your reply that had 'script.pl'
before Amichai's post that had 'script.pl', instead showing your reply
as being in response to his ORIGINAL post that had 'lib.pl'.
Consequently, I assumed you had changed his text in your quoted reply.

Now Amichai's post that contains 'script.pl' has indeed shown up, and
I look and feel like a completely jerk, and rightly so.

Though my initial response to this has also not yet shown up on Google
Groups, it basically calls you a liar (though in not so many words).
I very much profusley and humbly apologize for that reaction. I was
100% in the wrong, placing my faith in a service I know to be buggy as
all hell.

Sincerely,
Paul Lalli

Amichai Teumim

unread,
Aug 6, 2007, 9:17:41 AM8/6/07
to Mr. Shawn H. Corey, begi...@perl.org
No it was commented out. Now it's changed. I still don't know how to use
it. What would a good way to test it be?

echo 1234 | ./script.pl obviously won't do much. I wad told
./obj13-1.pl <<EOD

Yet that doesn't seem to work either.


Mr. Shawn H. Corey wrote:

Amichai Teumim

unread,
Aug 6, 2007, 10:34:48 AM8/6/07
to Mr. Shawn H. Corey, begi...@perl.org
I just renamed it to obj13-1.pl from script.pl

So it looks now like this:

#!/usr/bin/perl

use strict;
use warnings;

require 'obj13-lib.pl';

my @userArray = <STDIN>;

my $sum = sumIt(@userArray);

print $sum;

and the library I renamed to: obj13-lib.pl and now looks like this:


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

sub avg(@)
{
my @arr = @_;
my $arrSize = scalar(@arr);
(last index). Double check
return sumIt(@arr) / @arr;
}

1;

When I do the following:

:~$ perl obj13-1.pl

I get:

Not enough arguments for index at obj13-lib.pl line 11, near "index)"
Compilation failed in require at obj13-1.pl line 6.

What is this index error?


Mr. Shawn H. Corey wrote:

Chas Owens

unread,
Aug 6, 2007, 11:56:04 AM8/6/07
to Amichai Teumim, Mr. Shawn H. Corey, begi...@perl.org
On 8/6/07, Amichai Teumim <amichai...@idtglobal.com> wrote:
snip
> sub sumIt{
> my $total;
> $total += $_ for @_;
> warn "@_ was empty, total undefined!\n" if !defined $total;
> }
snip

This will return nothing useful. You need a return statement after the warn.

> sub avg(@)
> {
> my @arr = @_;
> my $arrSize = scalar(@arr);
> (last index). Double check
> return sumIt(@arr) / @arr;
> }

snip


> Not enough arguments for index at obj13-lib.pl line 11, near "index)"
> Compilation failed in require at obj13-1.pl line 6.
>
> What is this index error?

snip

You are getting that error because what used to be a comment is now

(last index). Double check

Next time you get an error message look at the line (in this case 11)
and see if there is anything obviously wrong with it. If there isn't
anything wrong with that line the problem is almost always above it.

Mr. Shawn H. Corey

unread,
Aug 6, 2007, 11:24:02 AM8/6/07
to Amichai Teumim, begi...@perl.org
Amichai Teumim wrote:
> I get:
>
> Not enough arguments for index at obj13-lib.pl line 11, near "index)"
> Compilation failed in require at obj13-1.pl line 6.
>
> What is this index error?

Go to line 11 in obj13-lib.pl and check its syntax.

A. Teumim - Netgear Support

unread,
Aug 7, 2007, 4:50:40 AM8/7/07
to begi...@perl.org
So this is what I have now in my library:


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

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

#(last index). Double check;
return sumIt(@arr) / @arr;
}

1;

Why is

return sumIt(@arr) / @arr;

wrong syntax?

It was suggested that I have to call the sumIt function.

Amichai Teumim

unread,
Aug 7, 2007, 4:26:17 AM8/7/07
to Chas Owens, begi...@perl.org
So that makes sense.

(last index). Double check

shouldn't even be there. I must have incompletely removed the comment. So I should just delete it or comment it out?

Amichai Teumim

unread,
Aug 7, 2007, 4:23:05 AM8/7/07
to begi...@perl.org

Line 11 n obj13-lib.pl is:

(last index). Double check

I don't know what could be wrong with this syntax, as I have never used
these commands before. I thought it migh need a curly, but I get the
same error.


Mr. Shawn H. Corey wrote:
> Amichai Teumim wrote:
>> I get:
>>

>> Not enough arguments for index at obj13-lib.pl line 11, near "index)"
>> Compilation failed in require at obj13-1.pl line 6.
>>
>> What is this index error?
>

Jeff Pang

unread,
Aug 7, 2007, 5:46:45 AM8/7/07
to begi...@perl.org

-----Original Message-----
>From: Amichai Teumim <amichai...@idtglobal.com>
>Sent: Aug 6, 2007 10:34 AM
>To: "Mr. Shawn H. Corey" <shawn...@magma.ca>
>Cc: begi...@perl.org
>Subject: Re: library (random numbers)
>
>I just renamed it to obj13-1.pl from script.pl
>
>So it looks now like this:
>
>#!/usr/bin/perl
>
>use strict;
>use warnings;
>
>require 'obj13-lib.pl';
>
>my @userArray = <STDIN>;
>
>my $sum = sumIt(@userArray);
>
>print $sum;
>
>and the library I renamed to: obj13-lib.pl and now looks like this:
>
>

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

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


>(last index). Double check

I don't know what's this line.Maybe it's a comment?

--
Jeff Pang <pa...@earthlink.net>
http://home.arcor.de/jeffpang/

Martin Barth

unread,
Aug 7, 2007, 6:25:33 AM8/7/07
to begi...@perl.org

it is a comment
you should delete it or
write a # in front of it.

Amichai Teumim

unread,
Aug 7, 2007, 6:11:24 AM8/7/07
to Jeff Pang, begi...@perl.org
Yeah I think it was a comment. I removed it now. Thanks ;)

Amichai Teumim

unread,
Aug 7, 2007, 5:22:36 AM8/7/07
to begi...@perl.org
Does this make any sense?

Here is the script again:

#!/usr/bin/perl

use strict;
use warnings;

require 'obj13-lib.pl';

my @userArray = <STDIN>;

my $sum = sumIt(@userArray);

print $sum;


And here is the library:

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

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

#(last index). Double check;

my $sum = sumIt(@userArray);

return sumIt(@arr) / @sum;
}

1;


Does the library make sense?

0 new messages