Phoenix.pm: Snippet that you might like...

Beaves at aol.com Beaves at aol.com
Wed Jun 23 00:27:13 CDT 1999


Before I post the snippet, let me cover a few things...
Is there going to be a meeting this Thursday?  If so, may I recommend a 
topic?  "Perl security issues and implementing secure transactions with Perl 
scripts."  I need to get up to speed on this stuff...hopefully sooner rather 
than later...

Also, I have an update to my LH module, and I am nearing completion of my 
'Tag' module, (Object oriented HTML generator).  I can give a simple demo of 
both at the next meeting if anyone is interested.

I'd like to see YOUR snippets.  I find them extremely useful.  There haven't 
been that many, but I have already used ideas from the ones that i have seen.

+++++  Tim's Snippet de Jour +++++

With code that I write, it is not that often that I write something general 
enough that I find it useful many times over.  This little bit of code has 
found a use in quite a few instances where I dereference lists, and that list 
may have other list references that I wan dereference.

Also, if an argument to your bit of code can take a scalar, or a reference to 
a list or scalars, then your probably used to writing:
my $inthing = shift;
my @list = (ref($inthing) eq 'ARRAY')?@$inthing:($inthing);

With this code, (even though it wan't written with this in mind) you can just 
call:
@list = &expand_list($inthing);
(Just keep in mind though that any nested list references will also get 
dereferenced.)


##############
# expand_list
#
# This sub will expand a list out of its references.
# $LR1 = ['LR1A', 'LR1B'];
# $LR2 = ['LR2A', $LR1];
# @list = &expand_list('ONE', $LR2, 'TWO');
#		#yields ('ONE', 'LR2A', 'LR1A', 'LR1B', 'TWO')
#
# If the argument in to expand_list does not contain any
# list references, then it does no expansion, as you would
# expect.
################

sub expand_list  {
	my @original_list = @_;
	local @ret_list;
	&push_or_expand(@original_list);
	return @ret_list;
	sub push_or_expand {
		my @in_list = @_;
		foreach $val (@in_list)  {
			if (ref($val) eq 'ARRAY') 
				{  &push_or_expand(@$val) }
			else {  push(@ret_list, $val)  }
		}
	} # end imbedded sub push_or_expand
} # end sub expand_list


Check Six!

Tim




More information about the Phoenix-pm mailing list