Zaid Zaid - 5 months ago 20
Perl Question

Why is "keys ::" not a syntax error?

I tried the following one-liner more out of curiosity than anything and was surprised that it actually worked without the

%
sigil.

$ perl -E 'say for keys ::'


It works on both versions 5.8.8 and 5.16.3; though the latter version emits this warning:


Hash %:: missing the % in argument of keys() at -e line 1.





How does this even work? What is so special about
%::
that allows it to run and print its keys, even without the sigil?

Note that the keys do not get printed with
%main::
.

$ perl -E 'say for keys main::'
Hash main:: missing the % in argument 1 of keys() at -e line 1.

Answer

TL;DR

:: isn't special; prior to Perl 5.22.0, you can omit the % and pass any identifier to keys.

However:

  • keys main:: is equivalent to keys %{'main'} or just keys %main
  • keys :: is equivalent to keys %{'main::'} or just keys %main::

The relevant code is in toke.c (the following is from 5.8.8):

/* Look for a subroutine with this name in current package,
   unless name is "Foo::", in which case Foo is a bearword
   (and a package name). */

if (len > 2 &&
    PL_tokenbuf[len - 2] == ':' && PL_tokenbuf[len - 1] == ':')
{
    if (ckWARN(WARN_BAREWORD) && ! gv_fetchpv(PL_tokenbuf, FALSE, SVt_PVHV))
        Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
            "Bareword \"%s\" refers to nonexistent package",
             PL_tokenbuf);
    len -= 2;
    PL_tokenbuf[len] = '\0';
    gv = Nullgv;
    gvp = 0;
}
else {
    len = 0;
    if (!gv)
        gv = gv_fetchpv(PL_tokenbuf, FALSE, SVt_PVCV);
}

/* if we saw a global override before, get the right name */

if (gvp) {
    sv = newSVpvn("CORE::GLOBAL::",14);
    sv_catpv(sv,PL_tokenbuf);
}
else {
    /* If len is 0, newSVpv does strlen(), which is correct.
       If len is non-zero, then it will be the true length,
       and so the scalar will be created correctly.  */
    sv = newSVpv(PL_tokenbuf,len);
}

len is the length of the current token.

  • If the token is main::, a new scalar is created with its PV (string component) set to main.

  • If the token is ::, a typeglob is fetched with gv_fetchpv.

gv_fetchpv lives in gv.c and has special logic for handling :::

if (*namend == ':')
    namend++;
namend++;
name = namend;
if (!*name)
    return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);

This fetches the typeglob stored in the default stash under key main:: (i.e. typeglob *main::).

Finally, keys expects its argument to be a hash, but if you pass it an identifier, it treats it as the name of a hash. See Perl_ck_fun in op.c:

case OA_HVREF:
    if (kid->op_type == OP_CONST &&
        (kid->op_private & OPpCONST_BARE))
    {
        char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
        OP * const newop = newHVREF(newGVOP(OP_GV, 0,
            gv_fetchpv(name, TRUE, SVt_PVHV) ));
        if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
            Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
                "Hash %%%s missing the %% in argument %"IVdf" of %s()",
                name, (IV)numargs, PL_op_desc[type]);
        op_free(kid);
        kid = newop;
        kid->op_sibling = sibl;
        *tokid = kid;
    }
    else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
        bad_type(numargs, "hash", PL_op_desc[type], kid);
    mod(kid, type);
    break;

This works for things other than ::, too:

$ perl -e'%h = (foo => "bar"); print for keys h'
foo

(As of 5.22.0, you're no longer allowed to omit the % sigil.)

You can also see this with B::Concise:

$ perl -MO=Concise -e'keys main::'
Hash %main missing the % in argument 1 of keys() at -e line 1.
6  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
5     <1> keys[t2] vK/1 ->6
4        <1> rv2hv[t1] lKRM/1 ->5
3           <$> gv(*main) s ->4
-e syntax OK
$ perl -MO=Concise -e'keys ::'
Hash %:: missing the % in argument 1 of keys() at -e line 1.
6  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
5     <1> keys[t2] vK/1 ->6
4        <1> rv2hv[t1] lKRM/1 ->5
3           <$> gv(*main::) s ->4
-e syntax OK