It turns out[*] that join accesses your separator each time it uses it (rather than making its own copy). So you can tie it and have join do something different each time it separates two elements. Note that a FETCH happens in the construction of the join expression.

* this is another way to say "undocumented behavior -- for recreational use only"

#!perl -l use strict; use warnings; package Tattle; sub TIESCALAR { return bless []; } sub FETCH { my $self = shift; my $thiscount = $self->[1]; ($self->[1] += 1) %= @{$self->[0]}; " $self->[0][$thiscount] "; } sub STORE { my $self = shift; @$self = (shift, 0); } package main; my @arr = ('a'..'g'); tie my $sep, 'Tattle'; $sep = ['*', '+', '-', '=']; print join $sep, @arr;

Replies are listed 'Best First'.
Re: Fun with join
by chanio (Priest) on Apr 03, 2005 at 05:54 UTC