Kaz Kylheku <
643-408-1753@kylheku.com> writes:
On 2024-10-10, Rainer Weikusat <rweikusat@talktalk.net> wrote:
Muttley@DastartdlyHQ.org ignorantly rambled:
On Thu, 10 Oct 2024 16:09:49 +0100
Rainer Weikusat <rweikusat@talktalk.net> boring babbled:
Muttley@DastartdlyHQ.org writes:
Its syntax is also a horrific mess.
>
Which means precisely what?
>
Far too much pointless punctuation. An interpreter shouldn't need the vartype
signified by $ or @ once its defined, it should already know.
>
For the purpose of variable declaration, how's the interpeter going to
>
Interpreter? Perl has some kind of compiler in it, right?
The Perl compiler turns Perl source code into a set of (that's a
conjecture of mine) so-called "op trees" whose nodes contain pointers to
built-in functions and pointers to "op subtrees" supplying arguments for
these and the interpeter/ virtual machine then evaluates these op trees
to run the program.
[...]
know the type of a variable without being told about it? Obviously, not
at all.
>
But it's not exactly type, because $x means "scalar variable of any
type" whereas @x is an "array of any type".
$x means 'scalar variable'. There's no furher differentiation of that at
the language level despite there are two kinds of scalar variables at
the implentation level, scalars whose values are "values" of some sort
(ie, strings or numbers) and scalars whose values are references to
something.
@x is an 1-D array of scalars.
That's quite useless for proper type checking and only causes noise,
due to having to be repeated.
>
Actually typed languages don't use sigils. How is that?
>
The type of a name is declared (or else inferred); references to that
name don't need to repeat that info.
The Perl type system is based on using different namespaces for
different types which means the type of a variable is part of its
name. This has the advantages that declaration syntax is concise and
that it's possible to have different kinds of variables with the same
name. It's also not really specific to Perl as C uses a similar model
for structures declarations and definitions.
The obvious disadvantage is that every variable name in Perl and every
use of a variable in an expression has and additional meta-information
character associated with it. The actual rules outside of declarations
are also more complicated because of the underlying idea that $ would be
something like a singular article in a spoken language an @ a plural
article. This means that elements of arrays and hashed are referred to
using a $ prefix and not @ or %, eg,
my @a;
$a[0] = 1;
or
my %h;
$h{goatonion} = 'winged cauliflower';
I think that's rather a weird than a great idea but it's internally
consistent and as good (or bad) as any other language ideosyncrasy. It's
certainly less confusing than the : as expression separator in
supposedly punctuation-free Python which tripped me up numerous times
when initially starting to write (some) Python code.
Things only start to get slightly awful when references become
involved. In Perl 4 (reportedly, I've never used that) a reference was a
variable holding the name of another variable, eg
$b = 1;
$a = 'b';
print $$a; # prints 1
Perl 5 added references as typed pointers with reference counting but
retained the symbolic referenc syntax. For the example above, that would
be
$b = 1;
$a = \$b;
print $$a; # also prints 1
Thinks start to become complicated once references to complex objects
are involved. Eg,
@{$$a[0]}
is the array referred to by the first item of the array $a refers to and
${$$a[0]}[0]
which seriously starts to look like a trench fortification with
barbed-wire obstacles is a way to refer to the first element of this
array. The {$$a[0]} is a block returning a reference to an array which
the surrounding $ and [0] then dereference. The { } could contain
arbitrary code returning a reference. But for the simple case of
dereference-chaining, this is not needed as it's implied for adjacent
subscript (for both hashes and arrays) which means the simpler
$$a[0][0]
is equivalent to the other expression.