Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Python gave me a colonic (solved - brainfart)

by bobn (Chaplain)
on Aug 24, 2020 at 22:38 UTC ( [id://11121059]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATE: else: is a label. my bad.

So I'm learning Python. I always disdained it because no curlies, but once you start doing stuff in it you get used to it. Sort of

But I still use Perl when I want to cobble something up quick. Using multiple languages has it's issues, though. I get confused. Here's one case where Perl seems to be confused, too, at 5.26.1. I installed perlbrew and it still happens at 5.032000 and 5.033001

#!/usr/bin/perl print "Perl version is: $]\n"; use strict; use warnings; if ( 1 ) { warn "there can only be one, and it's in the IF clause\n" } else: # beware that colon!!!! that's Python, not Perl!!! { warn "there can only be one, and it's in the ELSE clause\n" }

produces

Perl version is: 5.026001 there can only be one, and it's in the IF clause there can only be one, and it's in the ELSE clause Perl version is: 5.032000 there can only be one, and it's in the IF clause there can only be one, and it's in the ELSE clause Perl version is: 5.033001 there can only be one, and it's in the IF clause there can only be one, and it's in the ELSE clause

No warnings, no syntax errors, nothing except the (seemingly) impossible behavior

Removing the colon from the else statement restores correct behavior. gvim didn't catch it either - it's syntax highlighting is as if this was perfectly valid.

Is this a bug, or am i being persnickety?

--Bob

All code given here is UNTESTED unless otherwise stated.

Replies are listed 'Best First'.
Re: Python gave me a colonic
by Paladin (Vicar) on Aug 24, 2020 at 22:46 UTC
    It is valid code. else: is a label, and LABEL BLOCK is valid perl syntax.
      Correct, but

      > LABEL BLOCK is valid perl syntax.

      to be more precise

      LABEL: and { NAKED BLOCK } are already valid in isolation, they don't need to be compounded.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      OK, then, yes, it's a label. I saw else with a colon and didn't even think about labels, even though I've used them many times over the years.

      Actually, it seems a little odd that a keyword is permitted to be used in this fashion, but whatever.

      Thanks for the explanation. I'm gonna blame it on Guido, lol.

      --Bob Niederman, http://bob-n.com

      All code given here is UNTESTED unless otherwise stated.

Re: Python gave me a colonic
by LanX (Saint) on Aug 25, 2020 at 00:14 UTC
    To elaborate more ...

    > No warnings, no syntax errors, nothing except the (seemingly) impossible behavior

    Like already explained, because of the colon else: becomes a label, so it's quite possible.

    BUT it's good practice - or at least my practice - to only use uppercase WORDS for labels.

    So a linter could easily catch this already.

    Furthermore it's pretty hard to use a keyword like else as a label b/c next else; and alike would be syntax errors.

    To make it work you'd need to hide it inside an expression like with goto ("else")[0];

    So yes, there are potential ways to catch unwanted labels and warn about this. (lowercase and or reserved keyword)

    POC: this runs fine

    use strict; use warnings; my $x; else: { print ++$x; redo ("else")[0] if $x <3; }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    update

    in hindsight I realized that putting the label into quotes is already enough

    redo "else" if $x <3;

Re: Python gave me a colonic (solved - brainfart)
by perlfan (Vicar) on Aug 25, 2020 at 02:14 UTC
    >Python gave me a colonic

    LOL. Sounds like you got a labotomy to boot! This has given me some hilarious ideas for obfuscation research.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11121059]
Approved by Paladin
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-03-28 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found