Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:58:59+00:00 2026-06-18T14:58:59+00:00

As far as I know, in Perl, we can call a subroutine from a

  • 0

As far as I know, in Perl, we can call a subroutine from a Module by using these techniques:

  • Export subroutine foo, import the module which has this subroutine. Finally call it in your perl script.
  • Create an Object of that Module in your perl script finally call foo using that Object.
  • Directly call foo using its path, like this myDir::Module::foo();.

If I am always confused which is better way of calling a subroutine foo.
If I have a dynamic script, which I run from the browser and not command line, which approach one should go for so that the script takes less time.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:59:00+00:00Added an answer on June 18, 2026 at 2:59 pm

    There is a difference between the fastest, and the best way to call code in Perl.


    Edit: please see simbabques answer as well. He especially covers the differences between #1 and #3, and why you would use either.


    #1, #3: Function calls

    Your #1 and #3 are identical: The subroutine has an unique name in the globally visible namespace. Many names may map to one subroutine via aliases, or importing a module.

    If the name of the function you are calling is known at compile time, the sub will be resolved at compile time. This assumes that you don’t spontaneously redefine your functions. If the exact function is only known at runtime, this is only a hash lookup away.

    There are three ways how functions can be called:

    foo(@args);
    &foo(@args);
    @_ = @args; goto &foo;
    

    Number one (braces sometimes optional) is default, and validates your arguments against the sub prototype (don’t use prototypes). Also, a whole call stack frame (with much useful debug information) is constructed. This takes time.

    Number two skips the protoype verification, and assumes that you know what you are doing. This is slightly faster. I think this is sloppy style.

    Number three is a tail call. This returns from the current sub with the return value of foo. This is fast, as prototypes are ignored, and the current call stack frame can be reused. This isn’t useful very often, and has ugly syntax. Inlining the code is about an order of magnitude faster (i.e. in Perl, we prefer loops over recursion ☹).

    #2: Method calls

    The flexibility of OO comes at a hefty performance price: As the type of the object you call the message on is never known until runtime, the actual method can only be resolved at runtime.

    This means that $foo->bar() looks up the function bar in the package that $foo was blessed into. If it can’t be found there, it will be searched for in parent classes. This is slow. If you want to use OO, pay attention to shallow hierarchies (→ less lookups). Do also note that Perls default Method Resolution Order is unusual.

    You cannot generally reduce a method call to a function call, even if you know the type.

    If $foo if of class Foo, and Foo::bar is a sub, then Foo::bar($foo) will skip the method resultution, and might even work. However, this breaks encapsulation, and will break once Foo is subclassed. Also, this doesn’t work if Foo doesn’t define bar, but the method was defined in a parent class.

    I am generally in favour of object orientation, until it is clear from benchmarks that this will not provide the performance you require.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I know that FileSystemWatcher can look into a folder and if any
As far as I know de standard folder for Users (usually C:\Users) can be
I have a Perl question which should be fairly easy but can’t quite seem
As far as I know in perl global '/g' flag means that search will
As far as I know, only PHP can't be used for this. But since
I need to call a Perl script from a PHP file, and I've gotten
As far as know, I must be careful with PHP, and I think Javascript.
as far as I know both 2012-07-04T17:30:52+00:00 and 2012-07-04T17:30:52Z are dates in iso8601 format
As far as I know there is no way to migrate existing Facebook comments
As far as I know, C2DM is per app per device thing..but my question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.