Skip to content

Comment on Coding Intentionally in Bash Grainsparent

Comments

Why would anyone name their variable "_-1" ? Oh wait—

`_-1` is an invalid variable name.

The `-` must be followed by an alphabetic character (or `_`) for it to be seen as part of an identifier.

So `_-1` is the same as `_ - 1`

    {
      my \_ = 4;

      say _-1; # 3
    }

    {
      sub _ () { 8 }

      say _-1; # 7
    }
It may be a bad idea to name a variable or subroutine `_` but that is for you to decide, not for Perl6 to decide. (It's not your overprotective mother.)

---

I suppose if you really want to do something completely daft like that, there is not really anything stopping you:

    {
      my \_ = 3;
      say _-1; # 2

      my \term:<_-1> = my $ = 4;

      say _-1; # 4
      _-1 = 53;
      say _-1; # 53

      say _ -1; # 2
    }
Note that it doesn't just create a variable named `_-1`.

What it does is much more powerful than that. It modifies the parser lexically to add `_-1` as a term. (Since it is lexical it stops being valid after the closing `}`)

The `my $` is just so that it has a rewritable container so that it can be reassigned to `53` later.

This can be useful for constants that wouldn't otherwise be a valid identifier, and for writing subroutines that are parsed as a bare identifier like a constant would be.

    constant term:<> = …

    sub term:<foo> () {…}

    foo;

    foo(); # ERROR: Undeclared routine: foo used at line …

    foo 1; # ERROR: Two terms in a row
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.