in reply to calling a sub from an if statement

I believe that your msgs hash may be out of scope when you call msg. I tried your code and ran into the same problems. I rewrote it like this
my $msgs = { '200' => 'OK', '400' => 'Bad Request', '403' => 'Forbidden', '404' => 'Not Found', '500' => 'Internal Sever Error', }; sub msg { print "HTTP/1.0 $_[0] $msgs->{$_[0]}\n"; } if (1 == 1) { msg(400); }
And it seems to work fine.

On a side note you don't need double quotes around your hash vars, as they're not interpolated. Just something I learned and thought I'd pass on. It is supposedly a performance hit, although I've never tested it myself.

Replies are listed 'Best First'.
(jcwren) RE: Re: calling a sub from an if statement
by jcwren (Prior) on Jul 23, 2000 at 23:55 UTC
    Steve, you're code is definitely cleaner to look at, but I'm curious what problems you had running it the original. I copied it verbatim from the HTML, deleted the non-code, and ran it. I got two messages, one from the lone &msg(400), the other from the true if(), since $proto is undefined (no use strict and no -w. *gasp*).

    I then defined $proto with "HTTP/1.0", and got only one message, since the if() failed.

    What problems were you running into?

    --Chris

    e-mail jcwren