What is the difference between -> and :: in Perl

后端 未结 3 1659
北荒
北荒 2021-02-05 05:44

What is the exact difference between :: and -> in Perl?

-> sometimes works where :: does not.

3条回答
  •  一生所求
    2021-02-05 06:31

    When the right hand side is a function -> passes its left hand side as the first argument to the function. So the following examples are equivalent if $foo is an object blessed to package Foo and Bar is in package Foo. -> will resolve inherited methods making it cleaner and more useful for objects.

    $foo->Bar();
    
    Foo::Bar($foo);
    

    -> can also take a package name

    Foo->Bar();
    
    Foo::Bar('Foo');
    

    This means that -> is generally used in instance methods so that the object is passed its self and constructors so the constructors know which package to bless with. This is usually a parameter so it can be inherited.

提交回复
热议问题