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?
== is used when comparing numeric values. eq is used in comparing string values. = is the assignment operator, not a comparison operator.
!~ 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/)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With