[Kc] infix pointer question

David Nicol davidnicol at gmail.com
Thu Aug 31 14:01:19 PDT 2006


You appear to have a good handle on the subject you are asking
for an explanation of.  It is not clear to me what part of the
apparently clear source code you included in your question
is not clear to you.

Here's Dave's Intoductory Lecture on the "skinny arrow" operator
in Perl:

*  the syntax is taken from how C++ invokes methods on objects.

*  Between braces in multilevel containers, the -> is optional

* in perl 6, -> may be replaced with dot to look more like Java instead
of more like C.

> sub getmadein
> {
>         my ( $obj ) = @_;
>                 # Make a shortcut to the actual data hash.
>         # my $dh = $obj->{ 'data' };   #X
>         # return $dh->{ 'madein' };    #X
>         return $obj->{ 'data' }{ 'madein' };
> } # end sub getmadein

uncommenting the lines I marked #X should have merely
a very slight performance, and no semantic, effect.

> I've researched a little about this - it looks like 'infix' pointers
> dereference a reference??  I understand that
> a reference is a value that holds the location to another value but
> the syntax above just doesn't seem to make any sense to me.  Instead of
> using 'infix' pointers why not just return a regular scalar value or
> array?

It appears to me that the code you quote does exactly that:  the methods
you quote are access methods that pull specific data out of a complex
container.  The arguments for using explicit accessors are as follows:

    * abstract the data container implementation so it may be modified
in one place

    * code that calls an accessor is less noisy in context than code that
      directly accesses the data within the complex structure.

The parts of the perl distribution documentation that deal with these issues
are perlref, perlreftut, perllol, perldata, and others referenced w/in those.

Perl distributed documentation may be read at the command line interface
with "perldoc X" where X is the name of a documentation page; they
are also all available at http://perldoc.perl.org/


More information about the kc mailing list