Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: do separate hashes share common keys?

by jcb (Parson)
on Feb 14, 2021 at 03:08 UTC ( [id://11128352]=note: print w/replies, xml ) Need Help??


in reply to do separate hashes share common keys?

I am fairly sure that that is an optimization introduced in 5.8 to reduce memory usage with the common case of hashes used to implement objects. I believe that this optimization was part of the strategy for replacing the pseudo-hashes that were experimental in 5.6 and removed (if I recall correctly) in 5.10.

Pseudo-hashes were an attempt at reducing the overhead for structures with named fields by allowing multiple arrays to share a hashref in position zero which mapped keys to array indices.

  • Comment on Re: do separate hashes share common keys?

Replies are listed 'Best First'.
Re^2: do separate hashes share common keys?
by LanX (Saint) on Feb 14, 2021 at 13:29 UTC
    Pseudo-hashes were replaced by fields , right?

    I need to include fields in my benchmarks, it's strange almost nobody seems to use them.

    Either the benefits are not big enough, or (rather) the documentation is too confusing.

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

      Mark Jason Dominus gave a talk about the life and death of pseudohashes. (I’d heard of them but had no idea what they actually were before seeing this video.)

      I infer from it that fields.pm originated as a convenient interface for pseudohashes and that after they went away fields.pm was kept around mainly for backwards compatibility and reworked to use restricted hashes.

      I’d never played with fields.pm before, but I spent a few minutes with it just now and I can guess why it’s not very popular: it doesn't make much of an object. It doesn’t auto-generate accessors or a constructor. (Code is pasted below.) The attempted method call $blurple->blue blows up because there's no blue method. Also, the copy of %params into %{$self} will blow up if you pass any parameter name other than "red", "green", or "blue".

      – Aaron
      Preliminary operational tests were inconclusive. (The damn thing blew up.)
        you missed to declare "blurple" as of type RGBColor

        use strict; use warnings; warn "running"; package RGBColor { use fields qw(red green blue); sub new { my RGBColor $self = shift; my %params = @_; unless (ref $self) { $self = fields::new($self); } %{$self} = %params; return $self; } } my RGBColor $blurple = RGBColor->new(red => 75, blue => 200, green => +0); print "blurple's blue is $blurple->{blue}\n"; $blurple->new(red => 175, blue => 100, green => 100); print $blurple->{orange}; ### BANGS AT COMPILETIME print "blurple's blue is $blurple->{blue}\n";

        OUTPUT

        No such class field "orange" in variable $blurple of type RGBColor at +d:/tmp/pm/t_fields_pm.pl line 35. Compilation exited abnormally with code 255 at Sun Feb 14 23:04:10

        runs fine after uncommenting the orange line

        running at d:/tmp/pm/t_fields_pm.pl line 5. blurple's blue is 200 blurple's blue is 100

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

        From memory of old experiments:

        I think "fields" means attributes not methods.

        And it's supposed to "boom" if an attribute is missing or typoed, even at compile time.

        So it's rather a hash with frozen keys (not "Yet Another OO Model").

        But yes, the sparse documentation invites to misinterpretations.

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

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

    No recent polls found