From: sfryer <[EMAIL PROTECTED]>
> I'm reading perldoc perlref right now and under the Function Templates
> section, I've come across something that's got me stumped. The code in
> question is as follows...
> 
>    @colors = qw(red blue green yellow orange purple violet);
>    for my $name (@colors) {
>        no strict 'refs';      # allow symbol table manipulation
>        *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };
>    }
> 
> What I'd like to know is what is the purpose/meaning of the "uc" in
> *{uc $name} on the second last line? I couldn't locate an explanation
> for it anywhere.

And you do know what *$name is?

uc() is just an ordinary builtin function that uppercases the string 
you give it:

        print uc("hello WorLd!\n");

That's not the spicy part. The spicy part is the *. That denotes a 
typeglob. And you probably do not really want to know what the heck 
is that. In short typeglob is a structure that contains all package 
variables, the subroutine, the filehandle and the format of a given 
name. So

        *$name = sub {...};

creates a function whose name is in the $name variable.

        *{uc $name} = sub {...};
creates a function whose name is the uppercased contents of the $name 
variable.

The problem is that

        *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };

is not doing exactly what the author of the code most probably meant.

Try this:

        $name = "foo";
        *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };
        print foo(),"\n";
        print FOO(),"\n";
        # so far so good, but ...
        $foo = 5;
        print "$FOO\n";
        # uh oh

The problem is that the
        *foo = *FOO
aliases all parts for the *foo typeglob to their counterparts in 
*FOO. So $foo is the same as $FOO, @foo is the same as @FOO, %foo == 
%FOO, filehandle foo is the very same filehandle as FOO, etc.

Jenda
------------------------------------------------------------
All those who understood the topic I just elucidated, please 
verticaly extend your upper limbs.
        -- Ted Goff


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

Reply via email to