Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Checking for Arguments before Usage?

by nickcave25 (Acolyte)
on Feb 09, 2000 at 10:41 UTC ( [id://3241]=perlquestion: print w/replies, xml ) Need Help??

nickcave25 has asked for the wisdom of the Perl Monks concerning the following question:

Is it required (or at least a good idea) to confirm that arguments feeding into subroutines are actually specified?

If so, how do you do it?

This situation is giving me problems:

foreach $message (@logmessages) { LI(); Strong($message); } sub LI { #if (@_) {my($inputValue) = $_0;} print "<LI>$_0\n"; }
Note: the commented-out statement is my attempt at a solution for this quandry. And here's the quandry:

When this executes, I get a "Use of initialiized value" error regarding the reference to $_0 in the print command within the definition of LI().

I want to be able to both feed arguments into LI() (when I call it from other places) and _not_ feed in arguments (as in this instance).

Is there a way to set up that subroutine to allow for both cases?
TIA.

Replies are listed 'Best First'.
Re: Checking for Arguments before Usage?
by chromatic (Archbishop) on Feb 09, 2000 at 23:41 UTC
    In this case, you might also leave the subroutine if you don't get any arguments: return unless @_;
Re: Checking for Arguments before Usage?
by btrott (Parson) on Feb 09, 2000 at 23:28 UTC
    Or, if 0 is a valid value for the input to the subroutine (ie., you don't want to set $inputValue to the default if 0 is the arg), use:
    my $inputValue = defined $_[0] ? $_[0] : "default value";
Re: Checking for Arguments before Usage?
by nickcave25 (Acolyte) on Feb 10, 2000 at 04:50 UTC
    Thanks! I jotted down all 3 suggestions into the margin of page 95 of the llama book.
RE: Checking for Arguments before Usage?
by nickcave25 (Acolyte) on Feb 09, 2000 at 17:45 UTC
    But I guess my question still stands: Do you need to check for the presence of arguments before plugging them into the machinery within your subroutines?
Re: Checking for Arguments before Usage?
by nickcave25 (Acolyte) on Feb 09, 2000 at 17:49 UTC
    Make that $_[0] instead of $_0.
Re: Checking for Arguments before Usage?
by Anonymous Monk on Feb 09, 2000 at 23:23 UTC
    on your commented out line, try the following
    my $inputValue = $_[0] || "some default value in case of no argument" +;
    If there is a chance that your argument won't exist, always set up some kind of error checking or set a default value. the code i've used is a shorter version of an if:else construct.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found