in reply to strict refs on / off process time

Hi shemp,

Could it not be easier to cure the disease instead of camouflaging the symptoms? I don't know how much of a redesign of your code this would take, but why not work with anonymous functions and real references instead of symbolic references? E.g.:

use strict; my $funcref=sub { print "My camel is shopping for flea deterrent.\n"; }; &$funcref; # Note absence of no strict 'refs'

This way, there's no need to bounce up and down on the no strict 'refs'.

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: strict refs on / off process time
by shemp (Deacon) on Nov 18, 2002 at 23:14 UTC
    I'm not sure this will work in my situation.
    (Im not sure it wont either)
    I'm no namespace / symbol table expert!
    Anyway, here is what i'm doing. Im writing an engine for database migration / normalization. Our company receives data from about 100 (and growing) different sources, each providing data in different formats.
    The subroutines that are being called within the strict / no strict, are functions to change the format of individual fields from the raw source -> our common database. For example, addresses need to be parsed into street name, number, direction, bulding, unit, city, state, zip, etc. And since this info comes from so many different sources with wildly varying formats for just that field, I have a variety of parsing functions. So, im having the engine call main::address_parser_73(...) as necessary.
    (of course i'd never name a function address_parser_73).

    So, i don't see a good way to do the above on the fly for using one of hundreds of potential functions.
    But, as i said, i haven't done this sort of thing much - Im totally open to ideas.
    thanks
      Hi shemp,

      What you could do is just create your parser functions as normal named functions, but also create a reference to each of them. You can then store those references in an array and use an index into that array to find the correct function to execute. Untested example (pseudo)code for this could be:

      use strict; my @parsers; push @parsers, \&parser1; # Create a reference to parser 1 and push it + onto the array push @parsers, \&parser2; # Ditto for parser 2 ... sub parser1 { parse..parse..parse }; sub parser2 { parse_somewhat_differently };
      When a record from a source, say source 2 comes in, you access it's parser with:
      $parsers[$source_id]->( arguments ); # $source_id contains the source, + e.g. 2
      I have a feeling that you can even automate the process of creating the @parsers array by playing symbol table games, but I'm not awake enough any more to take a decent shot at it. Perhaps some more experienced (and awake) monk will step in here.

      CU
      Robartes-

      Store subrefs in a hash instead.
      my %parsers = ( address => sub { # ... }, f_dcompany => sub { # ... }, xmlbzzwrd => sub { # ... }, );
      Then you can call these like chromatic pointed out: $parser{$whatever}->(@pass, $some, @params);

      Makeshifts last the longest.