[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Copying hashes



>>push %$self, %$r;  # keys in %$r override keys in %$self
>>unshift %$self, %$r;  # keys in %$self override keys in %$r
>
>AFAIR, when this last came up on cplmoderated, the consensus was that
>it would be better for perl to recognize "%hash = %hash, LIST" and
>optimize it than to introduce new syntax.  Someone did come up with
>a patch to implement the push thing, though.

You can get this kind of override by doing this (although it's damn slow):

  %hash = (%values_get_overridden, %hash);
  %hash = (%hash, %values_override_hash);

This kind of "defaulting" can be seen in many object constructors:

  sub new {
    my $class = shift;
    my $self = {
      'this' => 'that',  # etc...
      @_,                # list to override defaults
    };
    return bless $self, $class;
  }

It's just a question of ordering.

-- 

  MIDN 4/C PINYAN, USNR, NROTCURPI     http://www.pobox.com/~japhy/
  jeff pinyan: japhy@pobox.com     perl stuff: japhy+perl@pobox.com
  CPAN ID: PINYAN  http://www.perl.com/CPAN/authors/id/P/PI/PINYAN/
  PerlMonth - An Online Perl Magazine     http://www.perlmonth.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe