On Mon, Jul 22, 2002 at 11:13:23PM -0700, nkuipers wrote:
> What I am about to raise, I do so more out of wanting someone to educate me 
> than disagreeing for the sake of.  The *that syntax shown below is unfamiliar 
> to me outside the context of referencing a filehandle in a subroutine params 
> list, where it is optional (ie., &some_sub(*STDIN)).  *that reminds me an 
> awful lot of a C pointer, however.  Can someone clear this up for me?

*STDIN and *that are globs, which don't really equate to pointers in C. 
Each package variable (or global variable) in Perl gets a slot in a glob. 
That glob contains the references to each of the data types.

For example, I say:

    $foo = "bar";
    $baz = "quux";

There were two globs created as a result of that, *foo and *baz.  These
globs can be used to reference those variables.

Now, if I say:

    %foo = (key1 => 'value1');

That hash was inserted into a slot of the glob *foo, namely the HASH slot.

So, basically, a glob is a way of referencing any data type by the same
name, be it a hash, array, scalar, etc.  The reason you're used to seeing it
with regard to filehandles is because a filehandle doesn't have its own
sigil (such as $, %, @), so you see people often prefix the filehandle with
the *.  However, this is generally not necessary, as the Perl builtin
routines that deal with filehandles know how to deal with a bareword as a
filehandle.

The aliasing (*this = *that) works because you're basically telling Perl
that the name "that" is a synonym for "this".  So, anytime you say $this it
looks up $that, say @this it looks up @that, and so on.  Because of this, I
prefer to target my aliasing by saying *this = \$that; what this does is
alias the scalar slot, but none other.  In this case, when you say $this it
looks up $that, but when you say @this it looks up precisely @this.


> I found the following in the Camel 3rd edition, page 257, which, though I may 
> be wrong, looks like it does what is requested?
> 
> @reflist = \(@x); #interpolate array, then get refs
> 
> which looks an awful lot like
> 
> @array2 = \(@array1);

Given:
    @array1 = qw(foo bar baz quux);

then:
    @array2 = \(@array1);

would be equivalent to:
    @array2 = (\$array[0], \$array1[1], \$array1[2], \$array1[3]);

Changes to @array1 elements, such as $array1[0] =~ s/f/z/g, would indeed be
reflected in the contents of @array2.  However, changes to @array1 itself,
such as adding elements (pop @array1), or setting an index ($array1[2] =
"quuux") would not be reflected in @array2.  This is because @array2
contains references to the -elements- of @array1, but it is not an alias for
@array1.

Surely the book explained this.

 
Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

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

Reply via email to