Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.unix.shell,comp.unix.programmer,comp.lang.misc Subject: Re: Command Languages Versus Programming Languages Date: Fri, 11 Oct 2024 16:15:14 +0100 Lines: 98 Message-ID: <87frp23c3h.fsf@doppelsaurus.mobileactivedefense.com> References: <87edbtz43p.fsf@tudado.org> <0d2cnVzOmbD6f4z7nZ2dnZfqnPudnZ2d@brightview.co.uk> <87a5fdj7f2.fsf@doppelsaurus.mobileactivedefense.com> <87wmighu4i.fsf@doppelsaurus.mobileactivedefense.com> <87o73rj3sr.fsf@doppelsaurus.mobileactivedefense.com> Mime-Version: 1.0 Content-Type: text/plain X-Trace: individual.net LYedjN1Ajfsmr0P+G4bJzQ1fN/MLHcnnjhO46Hn3k3TbHGCbo= Cancel-Lock: sha1:aq+VHmmntaC4EYsVjN8/kyMxmCs= sha1:HNJLHcwRvMgkqCW6xK301oXIY7A= sha256:95rzEH3RtmiMS5FLQiLApoclCK1wRh0UWdv9BwPcBCE= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Xref: csiph.com comp.unix.shell:25743 comp.unix.programmer:16372 comp.lang.misc:10917 Bart writes: > On 10/10/2024 17:55, Rainer Weikusat wrote: >> Muttley@DastartdlyHQ.org ignorantly rambled: >>> On Thu, 10 Oct 2024 16:09:49 +0100 >>> Rainer Weikusat 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 >> know the type of a variable without being told about it? Obviously, not >> at all. >> Perl has three builtin types, scalars, arrays and hashes and >> each is denoted by a single-letter prefix which effectively creates >> three different variable namespaces, one for each type. That's often >> convenient, because the same name can be reused for a variable of a >> different type, eg: >> my ($data, @data, %data); > > Why would you want to do this? Because it's convenient. It's possible to have a single variable denotning something, say $car, and also a variable use to hold a list of cars which could be named @car. >> $data = rand(128); >> @data = ($data, $data + 1); >> %data = map { $_, 15 } @data; >> it's also convenient to type and easy to read due to being concise. > > Adding shifted punctuation at the start of every instance of a > variable? I don't call that convenient! It's convenient for declarations, because it's the shortest possible syntax which can be used to declare the type of a variable. > So, $ is scalar, @ is an array, and % is a hash? Yes. >> Outside of declarations, $ and @ really denote access modes/ contexts, >> with $ standing for "a thing" and @ for "a number of things", eg >> $a[0] > >> is the first element of the array @a and > > Now I'm already lost. 'a' is an array, but it's being used with $? > What would just this: > > a[0] > > mean by itself? Nothing, ie, it's a syntax error. >> @a[-3 .. -1] >> is a list composed of the three last elements of @a. > > Sorry, these prefixes look utterly pointless to me. This stuff works > perfectly well in other languages without them. > > I can write a[i..j] in mine and I know that it yields a slice. But presumably only if a is actually an array. In Perl, it's a so-called bareword which was historically just a string. In current Perl, it's either a filehandle (which can't be indexed) or calling a function named a (which also can't be indexed). [...] > What would $a[-3 .. -1] mean? $a[0] In list context (denoted by @) the .. operator returns a sequence of numbers starting from the value on the left and ending with the value on the right. But because of the $, it's in scalar context. Then, it returns a boolean value which is false until the left operand becomes true, then remains true until the right operand becomes true and then again becomes false. That's supposed to be used for matching ranges of something. If an operand is a constant expression, it's supposed to refer to a line number of the last file which was accessed. Eg, perl -ne 'print if 10 .. 15' What happens if you have an array of mixed scalars, arrays and hashes; > what prefix to use in front of a[i]? Elements of arrays or hashes are always scalars.