Re: Reverse If and Normal Else
On Sep 22, Frank Geueke, III said: display_nothing() if ($match_type eq 'none'); else { } Now I like the reverse if because it takes up one line instead of four (I like braces on their own lines - see else). But this error... syntax error at /usr2/login/fjg/hotspot_tracker/search_by_ip_or_mac.cgi line 70, near else ...keeps coming up. Is there a way to keep the reverse if and use an else (or something like it) that anyone knows of? Thanks. ~Frank No, there isn't. Not with 'else', at least. You could use a ternary (hook) operator: ($match_type eq 'none') ? display_nothing() : display_something(); is like saying if ($match_type eq 'none') { display_nothing(); } else { display_something(); } -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reverse If and Normal Else
Frank Geueke, III wrote: Hi everyone. Okay, so maybe this one is a silly question. I have a fairly large script and I have a bunch of places where I'm following a reverse if with a normal else and perl keeps complaining about it. It seems to make sense to me, but I guess its bad syntax. Here is one of them: display_nothing() if ($match_type eq 'none'); else { } Now I like the reverse if because it takes up one line instead of four (I like braces on their own lines - see else). But this error... Not really because the ; is a statement terminator. If it is really about the number of lines there is nothing preventing you from just putting it all on one line. if ($match_type eq 'none') { display_nothing(); } else { } Personally I would rather see it just written out, a couple lines make little difference if the rest of your code is structured well. I suppose you could also reverse the logic and then your display_nothing would get shoved to the bottom of the whole block which is typically what I would do, so, if ($match_type ne 'none') { } else { display_nothing(); } Or, unless ($match_type eq 'none') { } else { display_nothing(); } HTH, http://danconia.org syntax error at /usr2/login/fjg/hotspot_tracker/search_by_ip_or_mac.cgi line 70, near else ...keeps coming up. Is there a way to keep the reverse if and use an else (or something like it) that anyone knows of? Thanks. ~Frank -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reverse If and Normal Else
Frank Geueke, III wrote: Hi everyone. Hello, Okay, so maybe this one is a silly question. I have a fairly large script and I have a bunch of places where I'm following a reverse if with a normal else and perl keeps complaining about it. It seems to make sense to me, but I guess its bad syntax. Here is one of them: display_nothing() if ($match_type eq 'none'); else { } Now I like the reverse if because it takes up one line instead of four (I like braces on their own lines - see else). But this error... syntax error at /usr2/login/fjg/hotspot_tracker/search_by_ip_or_mac.cgi line 70, near else ...keeps coming up. Is there a way to keep the reverse if and use an else (or something like it) that anyone knows of? The if statement modifier is just another way to write a logical and statement: $ perl -MO=Deparse -e' display_nothing() if $match_type eq q/none/ ' display_nothing() if $match_type eq 'none'; -e syntax OK $ perl -MO=Deparse -e' $match_type eq q/none/ and display_nothing() ' display_nothing() if $match_type eq 'none'; -e syntax OK $ perl -MO=Deparse -e' $match_type eq q/none/ display_nothing() ' display_nothing() if $match_type eq 'none'; -e syntax OK John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reverse If and Normal Else
On Sep 22, John W. Krahn said: The if statement modifier is just another way to write a logical and statement: $ perl -MO=Deparse -e' display_nothing() if $match_type eq q/none/ ' display_nothing() if $match_type eq 'none'; -e syntax OK $ perl -MO=Deparse -e' $match_type eq q/none/ and display_nothing() ' display_nothing() if $match_type eq 'none'; -e syntax OK $ perl -MO=Deparse -e' $match_type eq q/none/ display_nothing() ' display_nothing() if $match_type eq 'none'; -e syntax OK An important difference, though, is that the if statement modifier takes an expression and produces a statement, so you can't store its return value somewhere. That is, $result = (do_this() if that()); is a syntax error. Not so in the case of '' and 'and': $result = (that() and do_this()); $result = (that() do_this()); However, you can turn any statement into an expression by using do { } around the statement: $result = do { do_this() if that(); }; -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response