Perl lexer: why does “<=>” eq “=” in the context of <=><=><=>?

  • A+
Category:Languages

I was just reading the secret pseudo-constants, namely the Space fleet constant

<=><=><=>   Space fleet                 0 

This seems to be because the outer <=> is doing something I don't understand. My question is why does

my $foo = <=>; 

Set $foo to =? Other non-alphanumerics seem to work too,

my $foo = <=>; my $foo = <->; my $foo = </>; 

But, alphanumerics don't...

my $foo = <a>; 

Moreover, the perlsecret pod is confusing to me,

Even though it looks like a sequence of three spaceship operators, only the middle ship is an actual spaceship. The two outer "spaceships" are actually calls to glob("=").

It doesn't seem to be the case either, as I can't make sense as to why, glob("=") would return =, but glob("a") would return undef -- even if there is a file called a in the current working directory.

What is Perl doing in both of these cases? I assume it's falling back to a literal if the thing inside the <> isn't an alphanumeric -- is that behavior supported?

 


my $foo = <=>; my $foo = <->; my $foo = </>; 

These are all interpreted as calls to File::Glob::glob(). Since there are no *, ?, or […] constructions in their arguments, they expand to the text passed in.

my $foo = <a>; 

This is interpreted as reading one line from a file handle called a.

Anyways. <=><=><=> behaves like glob("=") <=> glob("="). Since both sides evaluate to "=", they're equal, and the comparison returns 0.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: