Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: prob w/ gatekeeper subroutine

by GrandFather (Sage)
on Sep 29, 2010 at 20:54 UTC ( #862700=note: print w/replies, xml ) Need Help??

in reply to prob w/ gatekeeper subroutine

As others have suggested: Always use strictures (use strict; use warnings; - see The strictures, according to Seuss)!

You seem to be writing Perl circa 4.x rather than 5.x. There were many enhancements made to Perl between 4.x and 5.x with most of them improving robustness of code in various ways (using strictures is an example of that). A few things we do differently now:

  • Use chomp instead of chop. chop simply removes the last character - whatever it was. chomp removes the a trailing copy of the contents of $/ (the input record separator special variable), normally a \n.
  • You don't need to initialise variables - Perl always does that by setting them to undef.
  • Don't call subs using &. That has a special meaning and will most often result in surprises - surprises are generally bad.
  • Don't use global variables. In small programs it's not such an issue, but as programs grow global variables make it very difficult to figure out how things work.
True laziness is hard work

Replies are listed 'Best First'.
Re^2: prob w/ gatekeeper subroutine
by weglarz (Novice) on Sep 30, 2010 at 15:50 UTC

    Thanks for everyone's responses. I am actually using Perl v 5.8.8. Also, what would I call a subroutine with other than &? I am currently learning to use strict but as this is an assignment, I need to get it to work before the due date and I cannot spend the extra time on learning strict before I have to turn it in. Also, if I were to stop using global variables, how would I change the admin password in this program. At the moment I have a subroutine that changes the global variable $adminpass.

      Well, I don't want to write your assignment answer for you, so I'll give you some different code to ponder a while that may help answer some of your questions.

      use strict; use warnings; main (); # Only to ensure no global variables are used - look ma, no v +ariables sub main { my ($dogName, $color) = getDogData (); print "The $color dog's name is $dogName\n"; ($dogName, $color) = getDogData (3); print "The $color dog's name is $dogName\n"; } sub getDogData { my ($select) = @_; my @names = qw(woof k9 fred max); my @colors = qw(spotted white black brown); $select = rand (@names) if ! defined $select; return $names[$select], $colors[$select]; }


      The black dog's name is fred The brown dog's name is max

      Please note that using strictures is not something you do when you have time, it is something you do to save time! Strictures tell you you have done something wrong early and can save huge amounts of time that would otherwise be spent poking around in the dark after black beetles with your eyes closed.

      Update: In a follow up question weglarz asked 'What does the "qw" in "my @names = qw(woof k9 fred max);" do?'. See Quote-Like Operators in perlop for a description of Perl's various quote operators like qw.

      True laziness is hard work

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-02-29 04:56 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (128 votes). Check out past polls.