aseidas has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,
I have been looking in perldoc and perlfaq trying to find more info about typeglobs. I am just trying to see if the code below is an appropriate use and not an abuse of a typeglob ( I really want it to be okay) but don't want to use it if there are good reasons not to.

Obviously the example is simplistic, I would use it in roughly the same manner in the real code. It does work the way I expect and want it to work, i am just not familiar enough with perl to know what is really going on behind the scenes. Anyway here is the code:

#!/usr/bin/perl -w use strict; open FH, '/tmp/configfile' or die "Could not open file: $!\n"; my $foo=<FH>; close FH; my @foo=split(//, $foo); my $foo1=join("",@foo[0..2]); my $foo2=join("",@foo[3..5]); my $foo3=join("",@foo[6..16]); my $foo4=join("",@foo[17..19]); my $foo5=join("",@foo[20..24]); foo1(*$); foo2(*$); foo3(*$); foo4(*$); foo5(*$); sub foo1{ print "$foo1\n"; } sub foo2{ print "$foo2\n"; } sub foo3{ print "$foo3\n"; } sub foo4{ print "$foo4\n"; } sub foo5{ print "$foo5\n"; }



Here is the contents of /tmp/configfile:

123 abc abc@123.com lp2 12345

Here is the output of the script:
123
abc
abc@123.com
lp2
12345

Thanks in advance - Aseidas

Replies are listed 'Best First'.
Re: Appropriate use of typeglob
by vladb (Vicar) on Jul 11, 2002 at 02:58 UTC
    Oh my, oh my.. I beg for a pardon, but would you please consider first answering this question:

    WHY do you need to use the typeglob and what good does it do in your particular example!? Remove the useless '*$' parameter that you are passing to a bunch of 'foo#' subroutines and the code should still print out the same exact stuff! To begin with, you don't even make any use of the passed parameter inside neither of your subroutines.

    If you are attemping to play with typeglobs just because they appear to be a 'cool' thing to play with, please steer away from them. Otherwise, err... I'd like to see a piece of code of yours which indeed does make a good use of them.

    Update: aseidas, happy to see that you took my question seriously and were not repulsed by it's apparent 'coldness' ;).

    With regard to typeglobs, they are not entirely a bad thing. Sorry if that is how it came through from my initial reply. However, they could be easily abused, especially at the hands of an inexperienced or a novice or plain ignorant (not you, however :) perl programmer.

    In responce to your point #3, I should point out that passing variables to a subroutine in Perl is rather trivial. I would suggest you read some introduction books on Perl programming to get the idea. Also, one of the replies to your initial posts includes a good & simple example for you to look at.

    Cheers ;-)

    _____________________
    # Under Construction
      Okay I will answer your questions, but first I will say, in the way you responded to mine, you have answered it. I was trying to find out if typeglobs were 'evil' and I think you pretty much summed up in few words that they are.

      1.You are right, it wasn't doing any good in my example. The intention was to use it in pulling in a config with about 100 variables I though it might be helpful.

      2.I didn't intend to make use of the passed parameter in the subroutines yet, it was just a test.

      3.I wasn't trying to pass with the typeglob because I thought it was 'cool' I was honestly trying to solve a problem, the problem being passing several variables to a subroutine easily.

      4.Also I did follow your suggestion and deleted the useless *$ param and indeed you were correct it still generated the same ouput. Is that not how I would pass the typeglob?

      You absolutely did answer my question, I will steer clear of the evil typeglob from this point on.

      Thanks so much for the quick and direct response ! -Aseidas
Re: Appropriate use of typeglob
by Aristotle (Chancellor) on Jul 11, 2002 at 03:26 UTC
    I think you misunderstood both your own problem as well as the purpose of typeglobs. Guessing by your example, I would say you are trying to figure out how to pass variables to subroutines - but you wrote one subroutine each for every global variable, rather than a single function that takes the variable as parameter. If I'm guessing right, then that's what the bottom half of your code should look like:
    print_foo($foo1); print_foo($foo2); print_foo($foo3); print_foo($foo4); print_foo($foo5); sub print_foo { my ($var) = @_; print "$var\n"; }
    It's better to put subroutine definitions at the top of the program in order to reap the full benefit from strict though.

    Makeshifts last the longest.

Re: Appropriate use of typeglob
by bronto (Priest) on Jul 11, 2002 at 10:21 UTC

    In my humble opinion, references in Perl 5 made superfluous the usage of typeglobs for almost everyday task (for a sensible concept of everyday task :-)

    Of course, I am not saying typeglobs are unuseful or typeglobs are dead, I'd like to make it clear.

    i am just not familiar enough with perl to know what is really going on behind the scenes

    About that, you may want to read the chapter about typeglobs & C. in the O'Reilly book Advanced Perl Programming (the one with the black panther on the cover). Even if I don't like to know the details of the internals, I succeded in reading about typeglobs without getting asleep. The author may consider it a great success :-)

    Anyway, I would like to award with the "bronto's best typeglobs use prize" Mr.Damian Conway with the *{$AUTOLOAD} = sub { ... } trick I found in his book, "Object Oriented Perl". Now Mr.Conway has the only prize he didn't get before!

    Yes, I'm kidding!!!

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }