Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Some help

by tfrayner (Curate)
on Nov 04, 2001 at 01:30 UTC ( #123084=note: print w/replies, xml ) Need Help??

in reply to Some help with a lottery picking program


First off, nice bit of code. I only have a couple of points. I would change the newlines in the mailing block to this:

print MAIL "To: $email"; print MAIL "From: Lottery Generator\n"; print MAIL "Subject: Your Lottery Numbers\n\n";
since the <STDIN> call still has a \n and the blank line that this introduces can cause a problem for email clients. Use chomp if you want to strip that newline for clarity.

Secondly, someone's going to tell you so it might as well be me, use strict and warnings. They're really very good :-)

Update: As usual, when I tried applying my own advice, strict and warnings threw up a bunch of problems. Probably the most insightful thing I can say is that you maybe don't want to name three different variables (@lines, $lines and @line) quite such similar names as it leads to confusion.

Now, use strict requires that you be a bit more precise about the scope of your variables. I think the simplest thing here would be to declare those three variables as global (disclaimer: unfortunately I don't have time to go into passing variables into and out of functions. Hopefully someone else will, because globals aren't much fun in anything larger which you write). With that change, and a quick alteration in the $i variable for clarity (and to get around a warning when invoked with the -w warnings switch), I get this as the first part of your code:

#!/usr/bin/perl -w use strict; print "-------------------------------------------------------\n"; print "National Lottery Number Picker \n"; print "-------------------------------------------------------\n\n"; print "How many lines do you want to play?\n"; print "Lines.....:"; my @lines; my @line; my $lines = <STDIN>; my @numbers = (1..49); srand; print "\n"; my $i = 1; &chooseline(); sub chooseline { while ($i <= $lines) { &genline(); push @lines, @line; $i++; if ($i eq $lines) { print "\n\n"; last; } } print "\n\n"; }
With a few more mys here and there (left as an exercise to the reader :-)), this will work under strict and be a cleaner bit of code.

Have fun,


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://123084]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2020-10-22 21:24 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (230 votes). Check out past polls.