[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [coldsync-hackers] Ok, what am I doing wrong???



On Tue, 10 Jun 2003, Izzy Blacklock wrote:
> I'm trying to pass two arrays to a function like this:
>
>         # Run the dump function the user specified.
>         $DumpFunction{$HEADERS{'Target'}}->(\@NamesArray, \@parsedData );
>
> My function works when I do it this way:
>
> sub textDump
>         {
>         my ( $names, $data) = @_;
>         foreach ( @$data )
>                 {
>
>
> But I have no need for $names in this target function, so I'd like to do
> something like this:
>
> sub textDump
>         {
> 	shift;
>         foreach ( @$_ )
>                 {
>
> This doesn't work as I expected.  My understanding of shift is that it should
> take the first value off the list (in this case \@NamesArray) and return it
> (in this case drop it), leaving the rest of the list.  Shouldn't that leave
> $_ = \@parsedData?

	No. Within a function, 'shift' modifies @_, not $_. Those are two
separate variables. @_ is an array, and $_ is a scalar. Yes, Perl allows
you to have the same name for a scalar, an array, a hash, ...
	What you want is not $_, but the first element of @_, so use

	foreach (@{$_[0]})

	Just in passing: as a matter of style, I prefer to declare 'my'
variables at the top of a function, rather than using @_ directly. So I'd
write &textDump as:

	sub textDump
	{
		my $names = shift;	# Ref-to-array of names. Ignored
		my $data = shift;	# Ref-to-array of data.
		my $foo;		# Some other variable used later

		foreach my $item (@{$data})
		{
			...

I find that a block of "= shift"s helps to clarify what arguments the
function expects. YMMV. HTH.

-- 
Andrew Arensburger                      Actually, these _do_ represent the
arensb@ooblick.com                      opinions of ooblick.com!
                        Generic Tagline V 6.01
-- 
This message was sent through the coldsync-hackers mailing list.  To remove
yourself from this mailing list, send a message to majordomo@thedotin.net
with the words "unsubscribe coldsync-hackers" in the message body.  For more
information on Coldsync, send mail to coldsync-hackers-owner@thedotin.net.