Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^8: Thx, St. Larry, for the Beauty of Sigils

by shmem (Chancellor)
on Jul 27, 2019 at 14:52 UTC ( [id://11103510]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Thx, St. Larry, for the Beauty of Sigils
in thread Thx, St. Larry, for the Beauty of Sigils

blah

sigh... "plain variables" are "named variables".

$var = "Hello world!"; # \ @ary = qw(foo bar baz); # - plain %hash = qw(foo 1 bar 2 baz 3); # / $sref = \$var; # \ $aref = \@ary; # - references $href = \%hash; # /

References which aren't references of plain (or named) variables are anonymous.

E.g. we speak of $ary = [] as of an anonymous array which happens to be stored in the plain (or named) scalar variable $ary, be that stored in a symbol table or a pad.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^9: Thx, St. Larry, for the Beauty of Sigils
by LanX (Saint) on Jul 27, 2019 at 15:41 UTC
    Well yes that's how it's implemented in Perl - and other languages like Python or JS too - but doesn't help comparing languages.

    They all store references into their "namespaces" or "lexical pads"

    Perl for instance does a *array= [1,2,3] to create a package variable accessible as @array °

    Python and JS do essentially* the same thing!!!

    Only the syntax to access and operate those variables is different.

    the following is legal² JS code ³

    >> $a = [1,2,3] Array(3) [ 1, 2, 3 ] >> $a[0] // Perl: $a->[0] 1 >> $b = $a Array(3) [ 1, 2, 3 ] >> $b[0]=666 // Perl $b->[0] = 666 666 >> $a Array(3) [ 666, 2, 3 ]

    and "anonymous" only refers to the literal [array] or {hash} constructors not to the scalar on the LHS of an assignment.

    The difference is not "named" or "unnamed", but explicit or implicit dereferencing.

    If anything, references are the "plain" fundamental thing..

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    °) or scalars, but this can't be easily written *scalar = \1 is not the same thing

    *) JS has primitive values (read scalar) and objects (read blessed references), there is no explicit referencing only implicit.

    ²) JS has no sigils $ is a legal part of an identifier to allow "machine translations", jQuery is messing with that.

    ³) updated and extended

      Well yes that's how it's implemented in Perl - and other languages like Python or JS too - but doesn't help comparing languages.

      They all store references into their "namespaces" or "lexical pads"

      ...

      and anonymous" only refers to the literal array or {hash} constructors not to the scalar on the LHS of an assignment.

      perldata states:

      Values are usually referred to by name, or through a named reference.

      So the LHS in $ary = [1,2,3] is a named reference; it is a named variable. The RHS is an anonymous array, at least that is what is called throughout the perl documentation. So let's stick to that to avoid confusion.

      The difference is not "named" or "unnamed", but explicit or implicit dereferencing.

      Difference between languages, but not in perl.

      In perl, the scalar $ary may hold an anonymous array. But @ary is a named array. Perl does no implicit derefrencing, and so the syntax to access slots of the named array @ary necessarily has to be distinct from the one to access a slot of the anonymous array stored in the named scalar variable $ary - or of any anonymous array:

      my $result = [ func(@args) ]->[2];

      So in perl - at least that is how I understand perl for a couple of decades now - the difference is between named and anonymous variables. To get hold of an anonymous variable beyond a single statement, its reference has to be stored in a named scalar variable.

      But I'm not going on telling you stuff you know as well as I do, only to get reverse 'halb' back, and I won't argue any more, since there is no point but frustration. Let's close this sub-thread. Thank you.

      update

      °) or scalars, but this can't be easily written *scalar = \1 is not the same thing

      Of course not, because that makes a reference from a constant. A variable reference is needed to make it variable:

      *scalar = \do { my $x = 1 };

      The expression *scalar = \"foo bar" creates a package constant (readonly package variable) $scalar (but *scalar = \"Hello $world" doesn't, it's r/w :-).

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        I have trouble calling something "anonymous" which is accessible by a named variable.

        perlglossary states

        anonymous

        Used to describe a referent that is not directly accessible through a named variable. Such a referent must be indirectly accessible through at least one hard reference. When the last hard reference goes away, the anonymous referent is destroyed without pity.

        But I just recalled that Perl tends to use the label "__ANON__" for subs which were not bound to a package at creation time (i.e. created the usual way with sub name {} )

        DB<22> use Carp qw/confess/ DB<23> $inner = sub { confess "INNER" } DB<24> sub outer { $inner->() } DB<25> outer() INNER at (eval 32)[C:/Perl_524/lib/perl5db.pl:737] line 2. main::__ANON__[(eval 32)[C:/Perl_524/lib/perl5db.pl:737]:2]() +called at (eval 33)[C:/Perl_524/lib/perl5db.pl:737] line 2 main::outer() called at (eval 34)[C:/Perl_524/lib/perl5db.pl:7 +37] line 2 ... #shortened outer(); ... #shortened DB<26> *inner = $inner DB<27> inner() INNER at (eval 32)[C:/Perl_524/lib/perl5db.pl:737] line 2. main::__ANON__[(eval 32)[C:/Perl_524/lib/perl5db.pl:737]:2]() +called at (eval 36)[C:/Perl_524/lib/perl5db.pl:737] line 2 ... #shortened inner(); ... #shortened

        I think this is a confusing terminology which might have made sense in the time of Perl4.

        But I see your point now.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        PS: Would be interesting to know if lexical subs are handled correctly here. (UPDATE: yes they are)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103510]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-24 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found