develooper Front page | perl.perl5.porters | Postings from July 2000

proposed extension to join

Wolfgang Laun
July 25, 2000 23:33
proposed extension to join
Message ID:

I'd be interested in hearing some opinions about this proposed extension
of join:

    Joins the separate strings of LIST into a single
    string with fields separated by the value of EXPR,
    and returns that new string.  Example:

    $rec = join(':',$login,$passwd,$uid,$gid,$gcos,$home,$shell);

|   If EXPR is an array reference, the elements of the
|   array will be used as separators in turn, and
|   repeatedly, if necessary. Example:
|   ( @_ == 2 ) ? "$_[0] and $_[1]"
|               : join( [ (', ')x(@_-2), ', and ' ], @_ );
|   join( [], @list ) is the same as join( '', @list ).

Other examples:

# create an HTML table, $c columns
$tdel = [ ('<td>')x($c-1), "\n<tr><td>" ]; 
print '<table>';
print '<tr>', join( $tdel, @cells );
print '</table>;

# print a HTML definition list
print '<dl>';
print '<dt>',
      join( [ "\n<dd>", "\n<dt>" ],
            map { ( $_, $gloss{$_} ) } sort keys %gloss );
print "\n</dl>";

The pros and cons I can think of are these:

(+) Simplifies text generation in some applications.
(+) Some algorithms may result in faster Perl code.
(+) Easy to implement: only the join run time code is affected; almost
no runtime penalty for joins with a scalar delimiter.
(-) Adds complication to a simple core function.
(-) May give rise to slower Perl code in other cases.

IMO, the scale is slightly tipped in favor of this extension (but I
would not be stricken by grief if it is voted down).

-Wolfgang Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About