in reply to Subroutines within if statements

I think you mean:

if ($answer eq "add" || $answer eq "a") { &addlinks; } elsif ($answer eq "remove" || $answer eq "r") { &rmlinks; } else { exit 0; }

You're probably better off calling those as addlinks() and rmlinks() until you understand how the &sub calls are special (see perlsub).

Also, I recommend that you use strict; use warnings; until you know why you might not want strict and warnings.

Replies are listed 'Best First'.
Re^2: Subroutines within if statements
by lodin (Hermit) on Feb 21, 2008 at 23:48 UTC

    Nowadays you can use

    use 5.010; if ("$answer" ~~ [ 'add', 'a' ]) { ... }
    I explicitly stringify $answer because if $answer happens to be something else (like an object) the ~~ operator will act differently and I don't want that. On the contrary, I want to allow objects that may stringify to "add" or "a".

    lodin

Re^2: Subroutines within if statements
by wfsp (Abbot) on Feb 22, 2008 at 08:45 UTC
    ...until you know why you might not want strict and warnings.
    Why might I not want strict and warnings?

    I get twitchy seeing no warnings in a small block. When I see either commented out at the top of a script I hide behind the sofa. :-)

    What is it I don't know yet? Is there a secret handshake? :-)

      Why might I not want strict and warnings?

      I haven't written any significant code without them since I learned of them. (I write perl -e without them.) I don't have a case where I wouldn't want them.

      That said, I could imagine code that produces spurious warnings. I could further imagine some PHB who wants no error messages in a log somewhere.

      I've heard some say they turn off strict and warnings in production to avoid some overhead. I personally think this is a lousy way to gain performance.

      Like you, I don't like to see code without strict and warnings. I don't want to work on it. Nevertheless, other programmers have their own minds, and I want to respect that.

Re^2: Subroutines within if statements
by muizelaar (Sexton) on Feb 22, 2008 at 11:03 UTC
    That's great as soon as I saw your code I knew where I went wrong. :( Bit of along day :)