You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Garth Winter Webb <ga...@perijove.com> on 2002/08/07 05:14:58 UTC

Re: How do my handler call dynamically a module based the request input

You keep switching what you say you want to do.  On one hand, your
original example did this:

    my $result = A::${module_call}::C->(@params);

In this case, you are calling the function 'C' which is in the module
'A::${module_call}', but then you go on to say that you define the
scalar $c (now lower case):

    $c = {
       b=> {
         a => {
           a1 =>1,
           a2 => 2,
         },
         ....
       }
    };

These are not the same thing.  Now, if you want to use Steve's method,
you'll need to make your structure available as a subroutine; you can't
access variables in that way.  If your data is static you can do
something like:

    package A::Foo;
    use constant C => { b => { a => { a1 => 1, a2 => 2 } } };

and since the 'use constant' really creates a subroutine for you, you
can do something like this:

    my $class = "A::$my_pkg";
    my $value_hash = $class->C;

Or if your data is dynamic, you can create regular subroutine that
returns the correct data:

    package A::Foo;
    sub C {
        return { b => { a => { a1 => 1, a2 => 2 } } };
    }



Garth



On Tue, 2002-08-06 at 18:58, Harry Zhu wrote:
> Nope. Even simple code like following will give me error.
> 
> I have some code like:
> 
> ##### GL/B1.pm
> package GL::B1;
> use strict;
> use vars qw($c);
> $c = {
>   x => {
>    t => 1,
>  },
>  ...
> };
> 
> 1;
> 
> ##### GL/B.pm
> ##### GL/B.pm
> package GL::B;
> use strict;
> sub handler {
>   my $b = shift;
> 
> ##### it worked if "use strict" is commented out, otherwise run-time error;
>  my $c = "GL::${b}::c"; my %x = %{${$c}->{x}};
> 
>  ##### it worked, but needs "no strict"; a little bit awkward
> # my %x; {no strict; %x = %{${"GL::${b}::c"}->{x}}; }
> 
> ##### it worked under "use strict"; not usable if the B1 should be dynamical
> # my %x = %{$GL::B1::c->{x}};
> 
>   my $t = $x{t}*2;
>   print $t;
> 
> }
> 
> 1;
> 
> Compiled no problem with "use strict", but when I run
>  > perl -e 'use GL::B1 qw($c);use GL::B; &GL::B::handler();'
> 
> It gives the error:
> Can't use string ("GL::B1::c") as a SCALAR ref while "strict refs" in use at
> GL/B.pm line 7.
> 
> Seems the problem has nothing to do with "call different module
> dynamically". Although if not for the dynamical call, it would directly
> %x = %{$GL::B1::c->{x}};
> 
> What can I do to use always the "use strict" and handle such situation?
> 
> Harry
> 
> 
> ----- Original Message -----
> From: "Steve Piner" <>
> To: "Harry Zhu" <ha...@greatlodge.com>
> Cc: <mo...@perl.apache.org>
> Sent: Monday, August 05, 2002 3:33 PM
> Subject: Re: How do my handler call dynamically a module based the request
> input
> 
> 
> >
> > You could probably do the same thing that constructors do, i.e. pass the
> > name of the class in a variable:
> >
> > my $class = "A::$module_call";
> > $class->C(@params);
> >
> > That should work under 'use strict'. You might need to see what happens
> > with the first parameter in C though.
> >
> > Steve
> >
> >
> > Harry Zhu wrote:
> > >
> > > That will do the work, but the thing is I need add "no strict" before
> such
> > > statements, maybe in many places.
> > >
> > > I can also do like
> > > $result =  A::B1::C->(@params) if $module_call eq 'B1';
> > > $result =  A::B2::C->(@params) if $module_call eq 'B2';
> > > $result =  A::B3::C->(@params) if $module_call eq 'B3';
> > > ...
> > > but it's not flexible, even not feasible when more and more Bs added to
> the
> > > list.
> > > Is there any other way that seems less odd to do the similar dispatch
> work?
> > >
> > > Harry Zhu
> > >
> > > ----- Original Message -----
> > > From: "Robert Landrum" <rl...@aol.net>
> > > To: <mo...@perl.apache.org>
> > > Sent: Monday, August 05, 2002 2:43 PM
> > > Subject: Re: How do my handler call dynamically a module based the
> request
> > > input
> > >
> > > > On Mon, Aug 05, 2002 at 02:08:24PM -0700, Harry Zhu wrote:
> > > > > Some small tricks needed to pass the "strict" check, see if you perl
> > > guru out there give me some hint:
> > > > >
> > > > > Suppose I have module A::B1, A::B2, A::B3 with method C
> > > > >
> > > > > my old cgi would look something like
> > > > > sub D {
> > > > > my ($module_call, @params) = shift;
> > > > > my $result = A::${module_call}::C->(@params);
> > > > > ...
> > > >
> > > >
> > > > Hmm...  Seems odd to do things this way, but Ok.
> > > >
> > > > my $result;
> > > > { no strict;
> > > > $result = A::${module_call}::C->(@params);
> > > > }
> > > >
> > > > Rob
> > > > --
> > > > Robert Landrum
> > > > Systems Programmer
> > > >
> >
> > --
> > Steve Piner
> > Web Applications Developer
> > Marketview Limited
> > http://www.marketview.co.nz
> >
> 
> 



Re: How do my handler call dynamically a module based the requestinput

Posted by Harry Zhu <ha...@GreatLodge.COM>.
Actually I have both variables and functions defined parallelly in a set of
config modules, which will be dynamically used to assign the value or make
the function call based on a input parameter. I was wish the uniform way
  $module = shift;
  $x = ${"A::${module}::c}->{b};
  $y = ${"A::${module}::m}->(@param);

will work, of cause, under the "use strict". I also exported these variables
and functions, so I can do something like
  require "A/$module.pm";
  $x = $c->{b};
  $y = &m->(@param);

Seems the simplest solution is to convert the variables to functions if I
don't want the "no strict" added to my code.

Thanks a lot.

Harry


----- Original Message -----
From: "Garth Winter Webb" <ga...@perijove.com>
To: <mo...@perl.apache.org>
Cc: "Harry Zhu" <ha...@greatlodge.com>
Sent: Tuesday, August 06, 2002 8:14 PM
Subject: Re: How do my handler call dynamically a module based the
requestinput


> You keep switching what you say you want to do.  On one hand, your
> original example did this:
>
>     my $result = A::${module_call}::C->(@params);
>
> In this case, you are calling the function 'C' which is in the module
> 'A::${module_call}', but then you go on to say that you define the
> scalar $c (now lower case):
>
>     $c = {
>        b=> {
>          a => {
>            a1 =>1,
>            a2 => 2,
>          },
>          ....
>        }
>     };
>
> These are not the same thing.  Now, if you want to use Steve's method,
> you'll need to make your structure available as a subroutine; you can't
> access variables in that way.  If your data is static you can do
> something like:
>
>     package A::Foo;
>     use constant C => { b => { a => { a1 => 1, a2 => 2 } } };
>
> and since the 'use constant' really creates a subroutine for you, you
> can do something like this:
>
>     my $class = "A::$my_pkg";
>     my $value_hash = $class->C;
>
> Or if your data is dynamic, you can create regular subroutine that
> returns the correct data:
>
>     package A::Foo;
>     sub C {
>         return { b => { a => { a1 => 1, a2 => 2 } } };
>     }
>
>
>
> Garth
>
>
>
> On Tue, 2002-08-06 at 18:58, Harry Zhu wrote:
> > Nope. Even simple code like following will give me error.
> >
> > I have some code like:
> >
> > ##### GL/B1.pm
> > package GL::B1;
> > use strict;
> > use vars qw($c);
> > $c = {
> >   x => {
> >    t => 1,
> >  },
> >  ...
> > };
> >
> > 1;
> >
> > ##### GL/B.pm
> > ##### GL/B.pm
> > package GL::B;
> > use strict;
> > sub handler {
> >   my $b = shift;
> >
> > ##### it worked if "use strict" is commented out, otherwise run-time
error;
> >  my $c = "GL::${b}::c"; my %x = %{${$c}->{x}};
> >
> >  ##### it worked, but needs "no strict"; a little bit awkward
> > # my %x; {no strict; %x = %{${"GL::${b}::c"}->{x}}; }
> >
> > ##### it worked under "use strict"; not usable if the B1 should be
dynamical
> > # my %x = %{$GL::B1::c->{x}};
> >
> >   my $t = $x{t}*2;
> >   print $t;
> >
> > }
> >
> > 1;
> >
> > Compiled no problem with "use strict", but when I run
> >  > perl -e 'use GL::B1 qw($c);use GL::B; &GL::B::handler();'
> >
> > It gives the error:
> > Can't use string ("GL::B1::c") as a SCALAR ref while "strict refs" in
use at
> > GL/B.pm line 7.
> >
> > Seems the problem has nothing to do with "call different module
> > dynamically". Although if not for the dynamical call, it would directly
> > %x = %{$GL::B1::c->{x}};
> >
> > What can I do to use always the "use strict" and handle such situation?
> >
> > Harry
> >
> >
> > ----- Original Message -----
> > From: "Steve Piner" <>
> > To: "Harry Zhu" <ha...@greatlodge.com>
> > Cc: <mo...@perl.apache.org>
> > Sent: Monday, August 05, 2002 3:33 PM
> > Subject: Re: How do my handler call dynamically a module based the
request
> > input
> >
> >
> > >
> > > You could probably do the same thing that constructors do, i.e. pass
the
> > > name of the class in a variable:
> > >
> > > my $class = "A::$module_call";
> > > $class->C(@params);
> > >
> > > That should work under 'use strict'. You might need to see what
happens
> > > with the first parameter in C though.
> > >
> > > Steve
> > >
> > >
> > > Harry Zhu wrote:
> > > >
> > > > That will do the work, but the thing is I need add "no strict"
before
> > such
> > > > statements, maybe in many places.
> > > >
> > > > I can also do like
> > > > $result =  A::B1::C->(@params) if $module_call eq 'B1';
> > > > $result =  A::B2::C->(@params) if $module_call eq 'B2';
> > > > $result =  A::B3::C->(@params) if $module_call eq 'B3';
> > > > ...
> > > > but it's not flexible, even not feasible when more and more Bs added
to
> > the
> > > > list.
> > > > Is there any other way that seems less odd to do the similar
dispatch
> > work?
> > > >
> > > > Harry Zhu
> > > >
> > > > ----- Original Message -----
> > > > From: "Robert Landrum" <rl...@aol.net>
> > > > To: <mo...@perl.apache.org>
> > > > Sent: Monday, August 05, 2002 2:43 PM
> > > > Subject: Re: How do my handler call dynamically a module based the
> > request
> > > > input
> > > >
> > > > > On Mon, Aug 05, 2002 at 02:08:24PM -0700, Harry Zhu wrote:
> > > > > > Some small tricks needed to pass the "strict" check, see if you
perl
> > > > guru out there give me some hint:
> > > > > >
> > > > > > Suppose I have module A::B1, A::B2, A::B3 with method C
> > > > > >
> > > > > > my old cgi would look something like
> > > > > > sub D {
> > > > > > my ($module_call, @params) = shift;
> > > > > > my $result = A::${module_call}::C->(@params);
> > > > > > ...
> > > > >
> > > > >
> > > > > Hmm...  Seems odd to do things this way, but Ok.
> > > > >
> > > > > my $result;
> > > > > { no strict;
> > > > > $result = A::${module_call}::C->(@params);
> > > > > }
> > > > >
> > > > > Rob
> > > > > --
> > > > > Robert Landrum
> > > > > Systems Programmer
> > > > >
> > >
> > > --
> > > Steve Piner
> > > Web Applications Developer
> > > Marketview Limited
> > > http://www.marketview.co.nz
> > >
> >
> >
>
>