Skip to content

Comment on A nice, little known C feature: Static array indices in parameter declarationsparent

Comments

Deleted my previous comment because it was wrong (I was saying that it didn't help with the names of subroutines). It appears you can even have subroutines named after keywords, but only as long as you put "&" in front of it when you call it:

    sub while
    {
        print "It works!\n";
    }

    &while();
I guess it is still the case that you can break perl code by adding keywords, if it collides with a subroutine that is called with the "&" symbol. Seems most code doesn't use "&" though, but it's a relatively quick fix.

Calling a sub with & has a special meaning, it exposes the current argument list @_ to the called sub. That's why you don't see it in most code.

I did not know that. Interesting. However, it only works if you call the sub without parentheses like this: &foo

Calling it with parentheses like &foo() would make @_ empty inside of foo. (Or if you said &foo("whatever") it would pass that as @_ instead.)

    sub while
    {
            print "@_\n";
            &foo();
    }

    sub foo
    {
            print "@_\n";
    }

    &while("a","b","c");
produces:
    a b c
    (blank line)
as the output, and
    sub while
    {
            print "@_\n";
            &foo;
    }

    sub foo
    {
            print "@_\n";
    }

    &while("a","b","c");
produces:
    a b c
    a b c 
as the output. At any rate, back to the original topic: it still doesn't prevent new keywords from potentially colliding. Oh well.
AboutSource Built by g1lg1l

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