Wouldn't it be nice to:
join %ENV, key_value_sep => '=', inter_pair_sep => "\n";

Replies are listed 'Best First'.
Re: join should polymorph on hashes
by chromatic (Archbishop) on May 02, 2001 at 00:31 UTC
    I'd rather have a map-like operator that can operate on an arbitrary number of elements at a time. Something like:

    my $items = join('', multimap( 2, { $_[0] . '=' . $_[1] . "\n" } %ENV));

    Maybe a little prettier, though.

      Like this?
      sub multimap (&$@) { my $fn = shift; my $n = shift; my @res; while (@_) { push @res, $fn->(splice (@_, 0, $n)); } @res; } print multimap { "$_[0]=$_[1]\n" } 2, %ENV;
      I tried to reverse the order of the first two arguments, but then the prototypes misbehaved. Reminded me of why I usually don't try to use them.
Re (tilly) 1: join should polymorph on hashes
by tilly (Archbishop) on May 02, 2001 at 03:02 UTC
    Sorry, I would find that useless.

    I spend very little of my time displaying native Perl data types. When I do need to do it, I usually want to do it with Data::Dumper. When I don't want to do it that way, I generally have some target output format which is generally not going to fit your simple display above. Which I could write already as:

    join "\n", map "$_=$ENV{$_}", sort keys %ENV;
    In fact even if your desired feature was added, I would deliberately never use it simply because looking up the key-word would be more energy than using the existing native flexibility to solve this problem simply and naturally.

    Plus the fact that this join interface bears no relationship to the usual join strikes me as a design decision that can do nothing but lead to grief later.

Re: join should polymorph on hashes
by satchboost (Scribe) on May 02, 2001 at 00:26 UTC
    I suppose ... but, saying something like

    sub hash_join { my ($hash_ref, $sep1, $sep2) = @_; my $string = ""; $string .= "${_}${sep}$hash_ref->{$_}${sep2}" for sort keys %$hash_ref; $string; } my $string = hash_join \%ENV, " = ", "\n"; print $string;

    works just as well, as well as having the benefit of being very obvious as to what you're doing. Your join would have to have too many new arguments, I think. But, that's just my opinion on the matter.

      I'd do:
      sub hash_join (\%$$) { my ($h, $between, $end) = @_; my $str; while (my($k,$v) = each %$h) { $str .= "$k$between$v$end"; } return $str; } print hash_join %ENV, "=", "\n";


      japhy -- Perl and Regex Hacker
Re: join should polymorph on hashes
by MeowChow (Vicar) on May 02, 2001 at 00:49 UTC
    Are you aware of the other common idioms for this sort of thing?
    %hash = (%hash, @more); %hash = (%hash, %more); @hash{@keys} = @values;
    Your hash join can be rewritten in the first form, though I usually try to arrange things so that the last one is possible.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      How are any of these creating a string representation of hash contents?
        I read your node as using join to append a list to hash. Now that I look at the names you chose for your hash keys, I can see what you meant more clearly; though I hadn't bothered to consider the names because I don't forsee any Perl built-ins using named parameters.
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
Re: join should polymorph on hashes
by princepawn (Parson) on May 02, 2001 at 01:02 UTC
    Or something like:
    use English; local $KEYVAL_SEP = '='; local $INTERPAIR_SEP = "\n"; my $join = "%hash";
    This would allow for use of string interpolation.