abigail on Mon, 10 Jul 2000 19:07:58 -0400 (EDT)


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

Re: references


On Mon, Jul 10, 2000 at 04:05:11PM -0400, Nicolai Rosen wrote:
> On Mon, 10 Jul 2000, Kyle Burton wrote:
> > > I've noticed that references don't tend to be used a whole lot in
> > > perl. Why is that? Wouldn't there be some tremendous gains in efficiency
> > > if they were? And how come no automagic referencing & dereferencing?
> > 
> > I'd have to agree with Michael, I use references for complex data structures
> > in almost all of the programs I've written using Perl.  If you've ever used
> > an object, you've used a refernece -- every object is a reference.
> > 
> > 
> > What gave you this impression?
> 
> Perhaps I should've been more specific. I didn't mean references involving
> objects (objects not in the every variable is an object sense). Obviously
> you see those everywhere. I've seen very little code involving \VARIABLE
> type stuff.


But \ isn't the only way to make a reference. [], {} and sub {} also
create references. And those, you see often.

As for \, there are a few cases where you can use \.

First, there is \$scalar. It isn't that often needed. In C, you often 
pass references to scalar variables to functions, so that the functions
can change the underlying value. That approach is much less needed in
Perl, for various reasons. First, sub routines can return lists, so if
you have to modify several variables in as sub, you can return the new
values, and do a list assignment. Secondly, there is already implicit
passing by reference.

    sub inc ($) {$_ [0] ++}
    my $foo = 3;
    inc $foo;
    print $foo;     # Prints 4.

\$scalar pops up in ties, objects, and the occational subroutine, like
in Getopt::Long::GetOptions ().

Then there are \@array and \%hash. But, if you are going to take a
reference to an array or a hash, often you are equally well off by
starting with a reference to anonymous array or hash. $arrayref = [];
and $hashref = {};. In other cases, you do have an array and a hash,
and you want to have a reference (for instance, you're putting them
into a hash or array as values), but don't want to share the reference.
\@array and \%hash would point to the original elements, so someone else
can modify them. Hence you might prefer [@array] and {%hash}.

There is also \&sub. Often used in callbacks, or in things like overload.
But again, Perl offers an alternative syntax: anonymous subroutines, which
look like normal subroutines, but they don't have a name.

    sub hello {print "Hello, world"}
    my $foo = \&hello;
    &$foo ();    # Prints "Hello, world"

vs

    my $bar = sub {print "Hello, world"};
    $bar -> ();  # Prints "Hello, world"


You could also take a reference to a type glob, as in \*GLOB. It's mostly
used to pass around file and dirhandles, but again, there are alternatives.
More alternatives in later versions of Perl. You'd also use references to
globs in you want to make object out of a handle.


There are lots of reasons to use references. But "tremendous gains in
efficiency" isn't a major one. 



Abigail
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**