On Nov 30, 2003, at 6:23 AM, Jason Dusek wrote:


What is a pointer and what is a reference?
How are they different?

As R.Joseph has noted they are different, and of course the obligatory perldoc tutorials. ( op. cit. )

That having been said, we might want to step back
to the 'older language' of

        "pass by reference" or "pass by value"
        "the Thing By Reference" or "the Thing in itself"
{ much nicer in german actually, they are one worders.
cf epistemology }

since this will give us a 'neutral base' to talk
about the problem in general.

When one notices that Perl really has only one
calling structure:

my @got_back = do_function(@arglist);

One passes in a 'list' or 'array' of stuff to the function
{ cf perldoc -q "What is the difference between a list and an array?" }
and it will return an array of stuff. This can be done
with 'naming the elements of the array' such as

my ($status, $line, $array_ref) = do_function($base,$domain);

but as folks notice, on the inside of that sub it will still
be about getting things off of @_  and it will fundamentally
be parking them on the Returning Array - eg:

        sub do_function
        {
                my ($b,$d,$s,$m) = @_;
                $s ||= s_default; # cf pragma for fun
                $m ||= m_default;
                ...
                return($flag, $answer, [EMAIL PROTECTED], [EMAIL PROTECTED]);
        }

Yes, that CAN be a bit hairy, but hey, why not?
Remember the Rules -
        'be nice to your subs and they will be nice to you'.

I opted for the odd structure, as it is a way to illustrate
a form of coding that can allow one to re-use a function
by adding additional options on the calling side, while
also showing that one can 'return' more than what the
caller would have 'asked for'.

IF all we have is One List In, and One List Out,
then the much desired for

my (@fat_list, @thin_list) = sort_lists(@list_a, @list_b);

IS SOOOO not going to work. TRY IT! write some code,
put it in the Debugger, See what Happens.

So the 'slick trick' is that we pass not the 'thing in itself'
but the Reference to the Thing, and we return the references.

my ($fat_list, $thin_list) = sort_lists([EMAIL PROTECTED], [EMAIL PROTECTED]);

The problem of course is that one IS passing in the Arrays not
in themselves, hence one is NOT copying the data of each
array into the @_ but merely the memory address of where the
array can be found. Which means that one is NOT working on a
copy of the lists!

That also means that one can SCRIBBLE on that memory space.
for fun try the gambit of

        my @list = qw/bob ted carol alice/;
        my @foo = qw/fred wilma barney fife/;
        my $gotArray = munge([EMAIL PROTECTED], [EMAIL PROTECTED]);

        sub munge
        {
                my ($a,$b) = @_;
                my @array = (@$a, @$b);
                @$b = ();
                [EMAIL PROTECTED];
        }

we know what @foo was before it went into the munge,
the question of interest is what will it be when
it gets munged....

Which we leave as an exercise for the reader.

In like Manner, that CAN be precisely what one really
wanted to do anyway and hence can do things like

        die "your house has burned down: $@"
                if ( initialize_me($casa) );

where that initialize_me() does the usual sort of

        sub initialize_me
        {
                my $house = shift;

                my $retval = clean_house($house)
                        if (ref($house));

                return($retval) if $retval;
                ....
                0;
        }

remember the question about having an initializer that would
be able to deal with 'deep structures'....

If I read a book on C++ pointers and references,
will it say the exact same things as a book on Perl references?

This is dangerous as there is 'language overloading' in what a 'reference' means in C++ that is beyond the scope of perl's use.

My recommendation of course is that you read it
in the original german, since they have a much
nicer habit of creating a Single Word for specific
things so as to avoid the habit in english of
re-using words in ways that can be confusing.

I mean if you see PanzerKampfWagon, there is
no question in your mind that it means 'tracked struggle wagon',
whereas in english 'tank' could be a verb for
too much gin and not enough sleep... as in

Oh dear, looks like drieux is tanked again...

which would be silly as

Oh dear, looks like drieux is PanzerKampfWagonEd again....

since one should not use a verb-ized Noun
in some un-natural way.... We have Morals you know....

The alternative of course is that you read about
coding language specification in their Actual ABNF structure
so that you understand the specific details of
how the tokens are assigned in the formal specification,
but unfortunately Perl is a bit too Promiscuous a
coding language, and wanders a bit hither and thither...

Your Mileage May vary, yada-yada-yada...

HTH.

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to