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

this is what i have i know it doesnt look right but i dont know whats wrong Thanks in advance
sub decide{ if ($answer =~ /^[Yy]/ ) { yes(); } else{ not(); } }

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: If/Else statement in subroutine. How do i place it?
by merlyn (Sage) on Sep 04, 2000 at 18:08 UTC
    Attempting to invoke a subroutine named not without an ampersand will almost certainly ruin a good day.

    Until you can detect when you are using a built-in word, use ampersands on all your subroutines, as ugly as it looks.

    Originally posted as a Categorized Answer.

Re: If/Else statement in subroutine. How do i place it?
by tilly (Archbishop) on Sep 04, 2000 at 17:02 UTC
    I think you are looking for perlstyle. The above, formatted in the usual Perl style would look like this:
    sub decide{ if ($answer =~ /^[Yy]/ ) { yes(); } else { not(); } }

    Originally posted as a Categorized Answer.

Re: If/Else statement in subroutine. How do i place it?
by ar0n (Priest) on Sep 04, 2000 at 12:38 UTC
    You can also use the ternary operator for simple evaluations such as these:
    sub decide() { $answer =~ m/^y/i ? yes() : not(); }

    Originally posted as a Categorized Answer.

Re: If/Else statement in subroutine. How do i place it?
by athomason (Curate) on Sep 04, 2000 at 07:48 UTC
    What's your question? Provided that yes and not are defined subs which do the appropriate things, this looks fine. If this code breaks, you'll need to provide more context (calling code, warnings, errors) before we can help.

    Originally posted as a Categorized Answer.

Re: If/Else statement in subroutine. How do i place it?
by BastardOperator (Monk) on Sep 06, 2000 at 02:52 UTC
    I assume that $answer has proper scope and that yes() and no() are correctly defined?

    Originally posted as a Categorized Answer.