[tpm] Is there a three-way version of...

Uri Guttman uri at StemSystems.com
Sat May 30 13:30:51 PDT 2009

>>>>> "SF" == Shaun Fryer <sfryer at sourcery.ca> writes:

  >>        foo() ;
  >>        foo( undef ) ;

  SF> Indeed!

  >> #       $self->{foo} = shift if @_ ;

  SF> I like that one. Short and sweet. The reason I was looking for a more
  SF> compact version is I find myself also often doing this kind of

i generally don't like using shift on @_ and prefer to assign it. for
accessors it makes a little more sense but i don't write them too often
and if i do, i automate creating them somehow.

  SF> evaluation on one or another type of nested reference. For instance.

  SF>     $$aref[$i][$j][$k] = $$href{foo}{bar}{baz} if defined $$href{foo}{bar}{baz};

  SF> Hopefully it's clear the merits of being able to do the following instead.

  SF>     $$aref[$i][$j][$k] =// $$href{foo}{bar}{baz};

you are missing a more important problem there. never duplicate deep
access code. use a temp var with the lower level reference and it will
be clearer and if you do need multiple uses, faster. 

also don't use $$aref[...]. instead use $aref-> as it is clearer as

so i would redo the above as:

	my $kref = \$aref->[$i][$j][$k] ;
	my $bazref = \$href->{foo}{bar}{baz};

	${$kref} = ${$bazref} if defined ${$bazref} ;

note that when you are doing deep accesses they are usually done
multiple times and commonly in loops. so you can factor out the higher
level access parts and do them before the loop. then the loop is cleaner
and faster as well. it is very hard to read and understand deep access
expressions so try to minimize their use with temps and other tricks.

  SF> Seeing you do "@array > 1" made me wonder about a particular style
  SF> issue. In the past I've done just that, but recently have started
  SF> explicitly doing "scalar(@array) > 0" alot more often (by default).
  SF> It's been my understanding that the "$#array" idiom is deprecated. If
  SF> there are arguments for/against each of the above, what are they?
  SF> Maybe I'm making a mountain out of a molehill for nothing...?

don't use scalar as it is redundant. arrays in scalar context return
their size which is all you need to know. when i see extraneous scalar()
calls like that i always mark it and let the author know. as for $#array
it is useful in the right context such as 0 .. $#array. it is not
deprecated but it is usually clearer to use @array when you want to deal
with the size of the array vs the last index. 

	@array > 1 
	$#array > 0

the former is better and easier to read. also in the bizarre case of
someone setting $] which changes the index origin, @array will stay the
same but $#array will change.


Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

More information about the toronto-pm mailing list