sub quote { local $_ = @_ ? $_[0] : $_; s/(['\\])/\\$1/g; return "'$_'"; } print map { "[$_];\n" } # Add brackets and tail. join ',', # Seperate elements with commas. map quote, # Quote elements. @arr; # Elements.