Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 662
NO WAR WITH RUSSIA Avatar asked Jul 06 '18 04:07

NO WAR WITH RUSSIA


People also ask

What is the difference between == and EQ in Perl?

== is used when comparing numeric values. eq is used in comparing string values. = is the assignment operator, not a comparison operator.

What does !~ Mean in Perl?

!~ is the negation of the binding operator =~ , like != is the negation of the operator == . The expression $foo !~ /bar/ is equivalent, but more concise, and sometimes more expressive, than the expression !($foo =~ /bar/)


1 Answers

Where an expression is expected, Perl picks the first of the following that applies:

  • <> is short for <ARGV>
  • <<>> is a "safe" version of <> (uses 3-arg open instead of 2-arg open)
  • << is otherwise the start of a here-doc operator.
  • <IDENTIFIER> is short for readline(IDENTIFIER)
  • <...> is otherwise short for glob(qq<...>)

Where an infix or postfix operator is expected, Perl picks the first of the following that applies:

  • <=> is the numerical comparison operator.
  • <= is otherwise the numerical less-than-or-equal operator.
  • << is the shift operator.
  • < is otherwise the numerical less-than operator.

(The last three might not be relevant here. I added them to cover everything starting with <.)

So,

  • <=><=><=>
    

    is short for

    glob(qq<=>) <=> glob(qq<=>)
    

    which can be written

    glob("=") <=> glob("=")
    

    or

    "=" <=> "="
    

    since a glob pattern with no special glob characters simply returns the pattern.

    It warns "isn't numeric" twice and evaluates to zero.

  • my $foo = <=>;
    

    is short for

    my $foo = glob(qq<=>);
    

    which can be written

    my $foo = glob("=");
    

    or

    my $foo = "=";
    
  • my $foo = <a>;
    

    is short for

    my $foo = readline(a);
    

    It warns "used only once: possible typo" if there are no other mentions of *a.

    It warns "on unopened filehandle" if you haven't previously opened a as a file handle.

like image 122
ikegami Avatar answered Sep 22 '22 21:09

ikegami