In a block you can do:

  no strict 'refs';
  my $mapref = \%{"$package\::map"};

And now you have a lexical reference that will bind to a closure.

Do note that using objects here is more common.

On Wednesday, October 29, 2014, Morse, Richard E.,MGH <
remo...@mgh.harvard.edu> wrote:

> Thanks -- I see how to do this with a string eval, but I'm hoping to avoid
> having to keep the functions as text strings.
>
> If I can't find any other way, it's what I will have to fall back on, but
> I'm hoping there's some way to avoid this.
>
> Ricky
>
> On Oct 29, 2014, at 2:56 PM, Ben Tilly <bti...@gmail.com <javascript:;>>
> wrote:
>
> > What our does is binds the package variable to lexical scope.  So a
> > package after an our doesn't change the variable.  But if you have the
> > our *after* the package then it will bind the correct package.  So in
> > your eval put the package statement before our %map and you'll be
> > fine.
> >
> > Incidentally if you are using eval in this horrible way, I strongly
> > recommend studying
> > http://perldoc.perl.org/perlsyn.html#Plain-Old-Comments-(Not!) for how
> > to make it clear in any error messages where your subroutines were
> > actually defined.
> >
> > On Wed, Oct 29, 2014 at 11:47 AM, Morse, Richard E.,MGH
> > <remo...@mgh.harvard.edu <javascript:;>> wrote:
> >> Hi! I'm running into an odd desire, and I'm hoping someone here can at
> least tell me where to look, as so far Google and DDG are not telling me
> what I want.
> >>
> >> I have a bunch of modules which have the same subroutines in each.
> Mostly, the code for the subroutines is the same, but there is a chance
> that any particular subroutine might be slightly different in any module
> (the context here involves internationalization). So, for instance:
> >>
> >>        package PKG::en_US;
> >>        our %map;
> >>        sub handle_ages { ; }
> >>        sub handle_dests { ; }
> >>
> >>        package PKG::fr;
> >>        our %map;
> >>        sub handle_ages { ; }
> >>        sub handle_dests { ; }
> >>
> >>        package PKG::pt_BR;
> >>        our %map;
> >>        sub handle_ages { ; }
> >>        sub handle_dests { ; }
> >>
> >> What I want to do is be able to create a base module, something like
> >>
> >>        package PKG::_base;
> >>        sub handle_ages { ; }
> >>        sub handle_dests { ; }
> >>
> >> I could then define the rest of them as
> >>
> >>        package PKG::en_US;
> >>        our %map;
> >>
> >>        package PKG::fr;
> >>        our %map;
> >>        sub handle_dests { ; }
> >>
> >>        package PKG::pt_BR;
> >>        our %map;
> >>
> >> Then use some kind of symbol table mungery to add the undefined
> functions to each package.
> >>
> >> This I can do.
> >>
> >> However, where I need help, is that I want to be able to have the
> package variable "%map" be properly used by the functions added to each
> package. That is, if I call PKG::en_US::handle_ages, it should use
> %PKG::en_US::map, not (the nonexistant) %PKG::_base::map.
> >>
> >> I've tried various things, but from what I can understand, even with
> everything declared "our", the sub definition closes over the package that
> it's in when defined. I've seen references to doing an `eval ("package
> $package; sub handle_ages { ; }")`, but this makes maintaining everything
> much harder, as I now don't have a base module, but rather a bunch of text
> strings.
> >>
> >> Thanks,
> >> Ricky
> >>
> >>
> >> The information in this e-mail is intended only for the person to whom
> it is
> >> addressed. If you believe this e-mail was sent to you in error and the
> e-mail
> >> contains patient information, please contact the Partners Compliance
> HelpLine at
> >> http://www.partners.org/complianceline . If the e-mail was sent to you
> in error
> >> but does not contain patient information, please contact the sender and
> properly
> >> dispose of the e-mail.
> >>
> >>
> >> _______________________________________________
> >> Boston-pm mailing list
> >> Boston-pm@mail.pm.org <javascript:;>
> >> http://mail.pm.org/mailman/listinfo/boston-pm
>
>

_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to