Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Modules to reduce syntax?

by LanX (Saint)
on Dec 22, 2021 at 14:44 UTC ( [id://11139833]=note: print w/replies, xml ) Need Help??


in reply to Re: Modules to reduce syntax?
in thread Modules to reduce syntax?

It's not clear to me what you want, since your code is wrong.

Here the correct analogous:

$ref->{foo}{$bar}

(NB: autoquoting of key "foo" !)

is

demo in the JS console

>> ref={foo: {x:42}} Object { foo: {…} } >> bar="x" "x" >> ref.foo[bar] 42 >> ref['foo'][bar] 42

as a side note, $ is a legal identifier in JS so you can also write

>> $ref={foo: {x:42}} Object { foo: {…} } >> $ref['foo'][$bar] 42

update

demo in the Perl debugger

DB<3> $ref = {foo=> { x => 42 } } DB<4> $bar = "x" DB<5> p $ref->{foo}{$bar} 42 DB<6> p $$ref{foo}{$bar} 42

update

DB<11> use experimental 'refaliasing'; \%ref = $ref DB<12> p $ref{foo}{$bar} 42

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^3: Modules to reduce syntax? (joining namespaces)
by LanX (Saint) on Dec 22, 2021 at 22:28 UTC
    You can't get rid of the sigils $%@ in Perl, because of conflicts with barewords which have many other meanings and the many bultin commands which depend on sigils.

    But this last example of aliasing a list-form and hashref-form \%hsh = $hsh could be simplified, such that the deref -> isn't needed anymore.

    use Keyword::Simple to define a new command like e.g. var or let and any

    var %hsh = LIST;

    is translated to

    my $hsh = \ my %hsh; %hsh = LIST;

    This is not too hard to implement, because you don't need to parse LIST in advance (which could be arbitrary Perl code)

    Like this explicit dereferencing with -> would be "simplified" away.

    Demo:

    DB<27> sub LIST { foo => { x => 42 } } DB<28> $bar ='x' DB<29> my $hsh = \ my %hsh; %hsh = LIST; \\ cont: say $hsh{foo}{$bar} 42

    Of course that way you'd loose the ability to have $ident and %ident in different namespaces, but that's often considered bad style anyway.

    And passing of hash-refs, like with function-parameters wouldn't be that easy without additional \ref-operation

    sub func { var (\%hsh,\@arr) = @_; }

    because otherwise var couldn't tell if a $scalar is meant to be aliased to a %hash or an @array

    sub func { var ($hsh,$arr) = @_; # ??? }

    tho you might be able simplify the syntax in many cases with

    sub func { var \(%hsh,@arr) = @_; }

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2025-05-20 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.