在 Perl 中从基类调用子类方法

发布于 2024-09-11 02:12:24 字数 1008 浏览 4 评论 0原文

我有一个名为 Mobile::Auth 的模块来授权并重定向到登录页面。我想访问 Mobile::AuthSite::Auth 的所有方法,但方法 redirect_to_login_page 除外,我有一个专门针对我的方法的方法。 移动::身份验证

我做了类似的事情...

package Mobile:Auth;
use base Site::Auth;

sub redirect_to_login_page{
  #get_my_mobile_specific
}
1;

在我的 Mason 组件文件中我放入了..

use Mobile::Auth;
Mobile::Auth::authorize($args);

这是我的 Site::Auth 看起来像

package Site::Auth;

....

   sub authorize {
     #.....
     if (!$authorize) {
       redirect_to_login_page($args);
     }
    }
    sub redirect_to_login_page{ 
     # redirect to the login page  
    }
    1;

授权工作,但我的问题是当我调用 authorize 时来自 Mobile::Auth 方法应该调用 Site::Auth::authorization 方法和 Mobile::Auth::redirect_to_login_page而不是 Site::Auth::redirect_to_login_page

伙计们,任何人都可以给我一个如何做到这一点的线索。提前致谢。

I have module called Mobile::Auth to authorize and redirect to the login page. I wanted to access all methods from Site::Auth in my Mobile::Auth except a method redirect_to_login_page, which I have specific one for my Mobile::Auth.

I did something like this...

package Mobile:Auth;
use base Site::Auth;

sub redirect_to_login_page{
  #get_my_mobile_specific
}
1;

and in my Mason component file I put..

use Mobile::Auth;
Mobile::Auth::authorize($args);

and here is how my Site::Auth looks like

package Site::Auth;

....

   sub authorize {
     #.....
     if (!$authorize) {
       redirect_to_login_page($args);
     }
    }
    sub redirect_to_login_page{ 
     # redirect to the login page  
    }
    1;

Authorization works, but my problem is when I call authorize method from Mobile::Auth it should call Site::Auth::authorization method and Mobile::Auth::redirect_to_login_page instead of Site::Auth::redirect_to_login_page

Guys, anyone can give me a clue how to do this. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无需解释 2024-09-18 02:12:24

Mobile::Auth 没有授权子项。

Mobile::Auth::authorize($args)

考虑到你所展示的,应该死。

正如 Daxim 指出的,您没有使用方法语法,因此没有调用 perl 的方法分派。您有两种选择来解决此问题。

第一种方法是调用您真正想要的子方法,

Site::Auth::authorize($args)

然后是

Mobile::Auth::redirect_to_login_page

但是,如果您尝试执行此 OO,而且我认为您是,您可以尝试包方法(这比对象方法不太常见,但是至少正确):

package Site::Auth;
#....
sub authorize {
    my ( $self, @args ) = @_;

    my $authorized = $self->validate_credentials(@args);

    if( !$authorized ) {
        $self->redirect_to_login_page(@args);
    }
}
sub redirect_to_login_page{ 
    my ( $self, @args ) = @_;
 # redirect to the login page 
}
sub validate_credentials {
    my ( $self, @args ) = @_;
    # This is what you had in #..... before
    return $authorized
}
1;

package Mobile:Auth;
use base 'Site::Auth';

sub redirect_to_login_page {
    my ( $self, @args ) = @_;
    #...
}
1;

### in Mason
use Mobile::Auth;
Mobile::Auth->authorize($args);

请注意一些更改:Site::Auth::authorize() 现在期望 $self 为第一个参数,Mobile::Auth 现在使用 ->; 调用授权。运算符,即方法调用语法。 :: 和 -> 之间的区别这里很大。首先,当你用->调用一个函数时,我们称它为“方法”而不是“子”。其次,该方法始终传递“$self”作为第一个参数。对于包方法,$self 只是一个包含包名称的字符串。对于对象,$self 是对该对象的引用。第三,使用您在此处尝试使用的 OO 层次结构来分派方法。

现在您会注意到,Mobile::Authorize 定义了自己的redirect_to_login_page(),但没有定义validate_credentials() 或authorize() 子函数。 (严格来说,您不必为接下来的内容考虑 validate_credentials() ,但您应该这样做,所以我这样做了。)

它是如何工作的? Mobile::Auth->authorize() 沿着链向上移动,直到找到 Site::Auth->authorize,然后调用它。 Site::Auth->authorize 将 $self 获取为“Mobile::Auth”。它调用 Mobile::Auth->validate_credentials,perl 最终将其分派为 Site::Auth->validate_credentials。然后它调用 Mobile::Auth->redirect_to_login_page,它实际上是在 Mobile::Auth 包中定义的,因此从那里调用它。

另外,您确实需要阅读http://perldoc.perl.org/ perlobj.html 从头到尾。这应该为您提供 Perl 中对象的基础知识。

Mobile::Auth doesn't have an authorize sub.

Mobile::Auth::authorize($args)

should die, given what you've shown.

As Daxim pointed out, you aren't using method syntax, and therefore aren't invoking perl's method dispatch. You have two options to fix this.

The first way is to call the that sub you actually want, which is

Site::Auth::authorize($args)

followed by

Mobile::Auth::redirect_to_login_page

However, if you're trying to do this OO, and I think you are, you could try package methods (which are less common than object methods, but at least correct):

package Site::Auth;
#....
sub authorize {
    my ( $self, @args ) = @_;

    my $authorized = $self->validate_credentials(@args);

    if( !$authorized ) {
        $self->redirect_to_login_page(@args);
    }
}
sub redirect_to_login_page{ 
    my ( $self, @args ) = @_;
 # redirect to the login page 
}
sub validate_credentials {
    my ( $self, @args ) = @_;
    # This is what you had in #..... before
    return $authorized
}
1;

package Mobile:Auth;
use base 'Site::Auth';

sub redirect_to_login_page {
    my ( $self, @args ) = @_;
    #...
}
1;

### in Mason
use Mobile::Auth;
Mobile::Auth->authorize($args);

Please note a few changes: Site::Auth::authorize() now expects $self to be the first argument, and Mobile::Auth now calls authorize with the -> operator, which is the method invocation syntax. The difference between :: and -> here is large. First of all, when you call a function with ->, we call it a "method" instead of a "sub". Second, the method is always passed "$self" as the first argument. In the case of a package method, $self is just a string containing the name of the package. In the case of an object, $self is a reference to the object. Third, methods are dispatched using the OO hierarchy that you are trying to use here.

Now you'll notice that Mobile::Authorize defines its own redirect_to_login_page() but does not define a validate_credentials() or authorize() sub. (Strictly speaking, you didn't have to factor out validate_credentials() for what follows, but you should, so I did.)

How does it work? Mobile::Auth->authorize() travels up the chain until it finds Site::Auth->authorize, then calls it. Site::Auth->authorize gets $self as "Mobile::Auth". It calls Mobile::Auth->validate_credentials, which perl eventually dispatches as Site::Auth->validate_credentials. It then calls Mobile::Auth->redirect_to_login_page, which is actually defined in package Mobile::Auth, so it gets called from there.

Also, you really need to read http://perldoc.perl.org/perlobj.html cover-to-cover. That should give you the basics on objects in perl.

习ぎ惯性依靠 2024-09-18 02:12:24

一个问题是您需要引用父类:

use base 'Site::Auth';

如果您有 use strict; 存在,那么您的代码就会出错:)

顺便说一句...您提到了 Moose 在您的标签中,但代码示例未使用它。

/I3az/

One problem is that you need to quote the parent class:

use base 'Site::Auth';

If you had use strict; present then you would have got an error with your code :)

BTW... you mention Moose in your tags but the code example doesn't use it.

/I3az/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文