Jenda Krynicky wrote:

> 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

So the writer fails in the attempt to further uglify the HTML produced.
Another samll problem, if this was used for generating web content, is the
choice of embedded quote operators.  At least on my installation, the
apostrophes remain apostrophes..  With small adjustments for that, and to
display some interim processing and output:

Greetings! E:\d_drive\perlStuff>perl -w
@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>" };
       print *$name, "\n";
   }
   my $line = RED('blue');
   print "$line\n";
^Z
*main::RED
*main::BLUE
*main::GREEN
*main::YELLOW
*main::ORANGE
*main::PURPLE
*main::VIOLET
<FONT COLOR="red">blue</FONT>

Greetings! E:\d_drive\perlStuff>

Joseph


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

Reply via email to