C<sub'a>?

M

Michele Dondi

A friend of mine has just written his first japh. A thing that I
couldn't understand at first[*], and that continued puzzling for quite
a while is a small fragment of code along the lines of

sub'a{that does something}

At first I was surprised that such a beast did even parse, but now I'm
highly confident I've understood why it does, although not
*completely* sure...

Well, here it comes: in earlier versions of perl you used e.g.
C<<$pkg'var>> instead of C<<$pkg::var>>, and that syntax is still
supported for backwards compatibility, so

sub'a

should be equivalent to

sub ::a # a.k.a. main::a

right?

Still assuming that my interpretation is correct, there remains one
subtle difference:

# perl -le "sub'a{9} print a"
9

# perl -le "sub::a{9} print a"
Can't call method "sub::a" without a package or object reference at
-e line 1.

# perl -le "sub ::a{9} print a"
9

So that it seems that perl disambiguates in different ways the two
constructs...


[*] And he doesn't, either, for he just copied that bit from some
other japh.


Michele
 
J

Juha Laiho

Michele Dondi said:
A friend of mine has just written his first japh. A thing that I
couldn't understand at first[*], and that continued puzzling for quite
a while is a small fragment of code along the lines of

sub'a{that does something}
Ok...

At first I was surprised that such a beast did even parse,

"Me too!"
but now I'm highly confident I've understood why it does, although not
*completely* sure...

Well, here it comes: in earlier versions of perl you used e.g.
C<<$pkg'var>> instead of C<<$pkg::var>>, and that syntax is still
supported for backwards compatibility, so

sub'a

should be equivalent to

sub ::a # a.k.a. main::a

Something akin to that would've been my guess as well - but the truth
seems different. Check what you get from the perl parser for each
of these:
perl -MO=Deparse -e "sub'a{9} print a;"
perl -MO=Deparse -e "sub::a{9} print a;"

The outputs are different, and I don't claim to understand each of them
properly (or, I do understand the output of the first one, but don't
understand how the parser ended up with that result); the second one I
don't understand as code - and even less as the parsing result.
 
T

Tad McClellan

Juha Laiho said:
Michele Dondi said:
A friend of mine has just written his first japh. A thing that I
couldn't understand at first[*], and that continued puzzling for quite
a while is a small fragment of code along the lines of

sub'a{that does something}


That is just the sub's definition, it doesn't really "do something"
until you _call_ that subroutine.

Something akin to that would've been my guess as well - but the truth
seems different.


No it doesn't, see below.

Check what you get from the perl parser for each
of these:
perl -MO=Deparse -e "sub'a{9} print a;"


There is the "base case".

perl -MO=Deparse -e "sub::a{9} print a;"

s/sub'a/sub ::a/ would be

perl -MO=Deparse -e "sub ::a{9} print a;"

which makes the "base case" output.

Without the space, that is not the "sub" keyword but a package named "sub".


s/sub'a/main::a/ would be

perl -MO=Deparse -e "main::a{9} print a;"

makes weird output, as it should, since there is no "sub" keyword there.

perl -MO=Deparse -e "sub main::a{9} print a;"

back to "base case" output again.

The outputs are different, and I don't claim to understand each of them
properly (or, I do understand the output of the first one, but don't
understand how the parser ended up with that result);


It defaulted to using the "main" package.
 
J

Jeff 'japhy' Pinyan

It defaulted to using the "main" package.

What is baffling her, Tad, is that

sub'name { ... }

is being seen as

sub ::name { ... }

instead of

sub::name { ... }

The question is *why* does the ' package separator in this case not act as
the :: package separator.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
 
A

Anno Siegel

Juha Laiho said:
Michele Dondi said:
A friend of mine has just written his first japh. A thing that I
couldn't understand at first[*], and that continued puzzling for quite
a while is a small fragment of code along the lines of

sub'a{that does something}
Ok...

At first I was surprised that such a beast did even parse,

"Me too!"
but now I'm highly confident I've understood why it does, although not
*completely* sure...

Well, here it comes: in earlier versions of perl you used e.g.
C<<$pkg'var>> instead of C<<$pkg::var>>, and that syntax is still
supported for backwards compatibility, so

sub'a

should be equivalent to

sub ::a # a.k.a. main::a

Something akin to that would've been my guess as well - but the truth
seems different. Check what you get from the perl parser for each
of these:
perl -MO=Deparse -e "sub'a{9} print a;"
perl -MO=Deparse -e "sub::a{9} print a;"

Put a blank between the sub keyword and the following name, that
removes an ambiguity. The second example is parsed entirely different
from what's intended.
Perl thinks "sub::a" is a method name and {9} is a block in indirect
object position (the lexer's catch-all, it seems). What follows are
parameters for the method call, only one in this case, namely what is
returned from "print a". At run-time this would result in a fatal
error. Perl will find that 9 is neither an object nor a class name,
so it can't call anything through it.

So don't be fooled by that result, it is bogus in this context.
"sub ::a{9}" and "sub 'a{9}" compile indeed the same code.

Anno
 
M

Michele Dondi

Something akin to that would've been my guess as well - but the truth
seems different. Check what you get from the perl parser for each
of these:
perl -MO=Deparse -e "sub'a{9} print a;"
perl -MO=Deparse -e "sub::a{9} print a;"

The outputs are different, and I don't claim to understand each of them

I think you'll find that in my original post I had made a remark
exactly about this point so that I must have been aware of this
circumstance. It is in the part of my post that you snipped...

However your (and mine) example only shows that perl parses

sub'a

as

sub ::a

and not as

sub::a

so that the two package separators behave differently.


Michele
 
M

Michele Dondi

What is baffling her, Tad, is that
^^^
^^^

<OT>
If "her" is referred to the OP, i.e. me, then it should be a "him"[*].
</OT>

However, yes that is what was baffling me. Apart that I was not 100%
(only, say, 99%) sure that it was the correct explanation at all...
sub'name { ... }

is being seen as

sub ::name { ... }

instead of

sub::name { ... }

The question is *why* does the ' package separator in this case not act as
the :: package separator.

Yes, actually the question remains, although it is not of vital
importance, I'd say.


[*] I've noticed that in english speaking environments people tends to
mistake my name, which is a masculine Italian name (equivalent say to
"Michael"), for the French "Michelle" that is a feminine name. Not
that this is really a big problem, but just wanted to point it out.


Michele
 
J

Jeff 'japhy' Pinyan

What is baffling her, Tad, is that

<OT>
If "her" is referred to the OP, i.e. me, then it should be a "him"[*].
</OT>

Sorry, Michele. Honest mistake. I'll be more careful next time.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,884
Members
47,419
Latest member
ArturoBres

Latest Threads

Top