Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Dereferencing in blessed object

by hippo (Bishop)
on Feb 26, 2021 at 09:39 UTC ( #11128823=note: print w/replies, xml ) Need Help??


in reply to Re^2: Dereferencing in blessed object
in thread Dereferencing in blessed object

Why is this preferable to $$vars{test}?

I don't see objectively why it would be, TBH. If you start getting into deeper structures then the increasing levels of syntax needed to deal with the $$foo{bar} equivalents is going to get messy. For a simple hash though there's little difference.

Personally I do use the arrow operator as it (subjectively) stands out more clearly to me in an eye-parse. But I do not quote hash keys (whether in references or otherwise) unless necessary. If I see code like $vars->{'test'} it makes me pause and wonder why the key is quoted.

Whatever you decide to do, make a plan and stick to it. Having your own coding standard, at least within one project, will be a benefit. Inconsistency is the bug's friend.


🦛

Replies are listed 'Best First'.
Re^4: Dereferencing in blessed object
by stevieb (Canon) on Feb 26, 2021 at 16:27 UTC
    "Personally I do use the arrow operator as it (subjectively) stands out more clearly to me in an eye-parse."

    Yep, me too.

    I like being able to understand code at a very quick glance, and at a quick glance, I would (and do) see $$var{thing} as a hash, not a hash reference. $var->{thing} is very unambiguous and doesn't require a second look, ever.

    Because scalar references are so infrequently used, I would expect to have to look twice at a variable with the double $$ to understand why the author has used one. An in-memory variable-based file is one example, and is a special case that often does require a bit more than just a glance.

Re^4: Dereferencing in blessed object
by LanX (Sage) on Feb 26, 2021 at 12:52 UTC
    > If you start getting into deeper structures then the increasing levels of syntax needed to deal with the $$foo{bar} equivalents is going to get messy.

    Could you give an example? I hope you are not referring to redundant -> arrows at deeper levels.

    > But I do not quote hash keys

    Neither do I, I just copy pasted from B::Deparse's output.

    edit

    > I don't see objectively why it would be,

    Like Bliako said, I have trouble parsing precedence, and I don't think it's only a matter training.

    can you immediately tell if $$x{key} means ${$x}{key} or ${$x{key}} ?

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

      Could you give an example? I hope you are not referring to redundant -> arrows at deeper levels.

      Quite the opposite, actually. By "equivalents" I was meaning the same syntax but for different data. eg. if we have $foo = { bar => [4, 5, 6] } and we want to operate on the array we can use @{$foo->{bar}} which is fine or @{$$foo{bar}} which, while valid, is a bit syntax heavy for my taste. (I like sigils but anything which starts off @{$$ is just asking to be clarified)

      can you immediately tell if $$x{key} means ${$x}{key} or ${$x{key}} ?

      I can but that's (a) from experience and (b) because sigils bind most tightly (as you said: precedence). It would not be a surprise to find that someone new to Perl would struggle with this too.

      Don't get me wrong - I agree with the presumed majority who use the arrow most/all of the time. However I do recognise it is a subjective choice and don't criticise others for avoiding it.


      🦛

        > @{$foo->{bar}} which is fine

        personally I'd prefer having autoboxing methods

        $foo->{bar}->list() (and $foo->{bar}->push() etc ...) cause it reads from left to right

        > or @{$$foo{bar}} which, while valid, is a bit syntax heavy

        And eye and logic have to hop back and forth

        Tangential:

        my dream would be able to have a pragma dualmy which is automatically aliasing each %IDENT with it's ref $IDENT and vice versa.

        Perl would become easier to read and to understand.

        (no implementation details, it's a dream... ;-)

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

      can you immediately tell if $$x{key} means ${$x}{key} or ${$x{key}} ?

      Personally I've just memorized this special case and yes, when I see $$x{key} or $$y[2] I just know this means $x->{key} or $y->[2]. I sometimes find it a bit nicer because of the parallels bewteen e.g. my %x = ( key => 42 ); print $x{key}; and my $x = { key => 42 }; print $$x{key}, but I also completely understand that people prefer $x->{key}, and I often use this style myself, for example I prefer $self->{key} over $$self{key}.

        I agree that the parallels are nicer. And I don't know if I could get used to it when I tried harder. (as I said PBP had it's influence on me)

        In any case I use @$href{a..c} for slicing references

        DB<42> @h{a..z}=1..26 DB<43> $hr=\%h DB<44> p @$hr{a..c} 123 DB<45>

        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://11128823]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-06-30 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (98 votes). Check out past polls.

    Notices?