bct has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm currently teaching a workshop for learning Perl. We've covered the basics and essentially followed through Learning Perl. We're into references and my students seem to be getting it, but yet I'm thinking they also don't seem to get it.

They understand how to create a ref and deref it,etc, but I think they are failing to appreciate the critical utility of references. I've used the array-flattening problem with subroutines, and we've gone over matrix representation, etc., but I ask the Monks, do you know of a particularly inspiring example to demonstrate the awesome power/necessity of references? Something that is sure to get the light turned on???

Any thoughts much appreciated...

BCT

  • Comment on Examples of the importance of references?

Replies are listed 'Best First'.
Re: Examples of the importance of references?
by GrandFather (Saint) on Mar 17, 2006 at 01:09 UTC

    Arrays and hashes only store scalars. If you want an AoA or HoA or any such interesting structure, you have to use references.


    DWIM is Perl's answer to Gödel
Re: Examples of the importance of references?
by revdiablo (Prior) on Mar 17, 2006 at 01:04 UTC

    The example in perlreftut is quite good, I think. I'm not even going to try and duplicate any of his work here. :-)

Re: Examples of the importance of references?
by spiritway (Vicar) on Mar 17, 2006 at 04:24 UTC

    You might tell them to create some sort of data structure that, using a customer number as a key, would hold their name, address, and phone number - or something along those lines. Let them sweat that for a while, then magically show them how utterly simple refs are (well, OK, maybe not so simple, but feasible). It seems like it's usually a good idea to let students sweat a problem for a while, to give them an idea of its difficulty, before presenting them with a decent solution.

Re: Examples of the importance of references?
by zer (Deacon) on Mar 17, 2006 at 01:12 UTC
    ive always liked the uglyness of perl. Try showing them how to combine arrays of hashes of scalars of u name it. You can make some impressive combinations. Its amazing how diverse you can make a single variable.

    typeglobs arent a bad example either. Demonstrate how all it is is a group of references.

    also refs of subroutines are pretty cool

Re: Examples of the importance of references?
by zentara (Cardinal) on Mar 17, 2006 at 12:03 UTC
    The best example I can think of, is when you generate a huge array to be returned from a sub. If you return the array, a huge copy is made and returned. If you return the array ref, you just return the memory location of the first array element. It is far more efficient just to return where the array is loacted, rather than the array itself.
    my $aref = get_values(); sub get_values{ my @array = (1..5000000); return \@array; }
    Of course for small arrays, it really dosn't matter.

    Another place you will see references used often is in objects. You don't want to pass around copies of objects, so you pass their memory location (reference). Inside the object, the object refers to itelf as $self, because it dosn't care where it is located.


    I'm not really a human, but I play one on earth. flash japh
Re: Examples of the importance of references?
by gex (Scribe) on Mar 17, 2006 at 06:29 UTC
    You could build an example based on

    1) call by reference vs call by value
     &mysub(\@data) vs &mysub(@data)

    2) return reference vs return value
     return \@xyz return @xyz

Re: Examples of the importance of references?
by johngg (Canon) on Mar 17, 2006 at 09:09 UTC
    Having got your students to craft some data structures using some of the suggestions from other esteemed Monks, you might consider getting them to output their structures using Data::Dumper. This could be a good way of illustrating the power of references and how they all hang together.

    Cheers,

    JohnGG

Re: Examples of the importance of references?
by planetscape (Chancellor) on Mar 17, 2006 at 17:31 UTC
Re: Examples of the importance of references?
by gaal (Parson) on Mar 17, 2006 at 11:32 UTC
    You can't understand Perl 5 OOP without understanding references.