Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Reply to replies and update

by bigmacbear (Monk)
on Aug 10, 2006 at 02:18 UTC ( [id://566530]=note: print w/replies, xml ) Need Help??


in reply to Reply to replies and update
in thread Confused about 'Name "main::whenfound" used only once:'

It appears you are trying to make one subroutine do the work of several, and distinguishing which by the number and content of your "subargs" (which I will call "parameters" to your sub, to distinguish them from the command line arguments in @ARGV). This one sub does five different things:

  • With no parameters passed, counts the number of arguments passed on the command line;
  • Otherwise, takes its first parameter and compares it against each command line argument, keeping a count of the number of matches and nonmatches;
  • If the second parameter matches a given string, returns the count of matches determined above;
  • If the second parameter matches a different string, returns the count of nonmatches given above; and
  • If the second parameter matches yet something else, tries to do something I can't figure out. Since this is never tested by the test cases given, I'm going to ignore this case entirely and leave it to you and to the other monks.

I would structure this monster as a number of different subroutines. Basically, get rid of the second parameter to your catch-all sub; then write one subroutine to do each of the five tasks above, using the sub's name to convey the same information. The first sub, however, is probably better written right into your mainline code, as it operates on the global @ARGV -- for now. The second sub would be called in turn by the following two or three.

Furthermore, I wouldn't necessarily rely on the fact that the list you are searching for resides in @ARGV, for the simple reason that a real program will very likely accept its data from a file or other source rather than as discrete command-line arguments. Copy the arguments into a normal array and pass a reference to this array to the task subroutines above. If you have no idea what I mean by references at the moment, click or run perldoc perlreftut.

Oh, and your uninitialized variables problems will pretty much Go Away if you do two things:

  • set all your "my" variables to a value (preferably 0 or "") when you declare them, and
  • instead of testing for defined (which you haven't quite got the hang of yet), just test for nonzero/non-null: if ($variable) instead of if (defined $variable).

You're getting there, just not quite there yet.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found