Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^7: Definition of numerically equal and rationale for 'you' == 'me'

by moritz (Cardinal)
on Mar 03, 2012 at 06:45 UTC ( [id://957612]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Definition of numerically equal and rationale for 'you' == 'me'
in thread Definition of numerically equal and rationale for 'you' == 'me'

I suppose the vast number of operators that Perl supports, compared to other languages, is made possible by sigils; that is, languages without sigils couldn't so easily use x or xx as an operator.

Technically that's not quite correct. The really interesting property that makes that possible is predictive parsing. That is, the Perl parsers always knows whether to expect an infix operator or something termish (a prefix operator or a term, like for example a number, variable, subroutine call etc.).

Even in an imaginary Perl without sigils, the following code could be parsed unambiguously:

# imaginary sigilless Perl my x = 42; say x x x;

The parser knows that say is a listop, and after that it expects a term. Thus the first x is term (here a variable), but since two terms in a row are forbidden, the next thing must be an infix operator (or maybe a semicolon to terminate the statement). Thus the second x is parsed as the repetition operator. And so on.

Predictive parsing is also what makes it possible to use the slash / both as an infix operator, and as a regex delimiter.

But even if it worked in theory, a sigil-less language with such a wealth of infix operators would probably be rather hard to read, and it would be much harder (if not impossible) to give good error messages in the case of syntax errors.

Replies are listed 'Best First'.
Re^8: Definition of numerically equal and rationale for 'you' == 'me'
by JavaFan (Canon) on Mar 03, 2012 at 08:45 UTC
    Even in an imaginary Perl without sigils, the following code could be parsed unambiguously:
    No, it cannot. Stepping away the additional problem with say (the first x can be seen as a filehandle), you're forgetting that sigils make barewords possible. You're x x x can be parsed in several ways:
    $x x $x x(x($x)) x::->x("x")
    or some combination there off. That is, if we'd dropped sigils, a bare x can be a (scalar) variable, an operator, a class name, a subroutine, or the string "x".

      Well, everything is possible with the right amount of disambiguation rules. Perl 6 doesn't have barewords, and type names must be predeclared. If a subroutine of name x and another symbol with that name (type name, constant, enum element) are in scope, the parser disambiguates in favor of the latter, unless there are parens after the token.

      In fact, constants can give you a taste of sigil-less Perl 6 already:

      use v6; constant x = 4; say x x x # produces 4444

      This works today in both Rakudo and Niecza.

        That works in Perl 5 as well:
        perl -Mconstant=x,1 -MO=Deparse -e 'print x x x' print '1' x '1'; -e syntax OK
        But:
        perl -MO=Deparse -e 'sub x {1} print x x x' sub x { 1; } print x(x(x())); -e syntax OK

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://957612]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found