Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am pretty new to perl, also i searched but no useful results. What is the difference between calling a function in a user defined ( my another module ) with mod_name::func_name and just func_name ? Can you please explain or point me to some place where i can read about it.
  • Comment on diff b/w func_name and module::func_name

Replies are listed 'Best First'.
Re: diff b/w func_name and module::func_name
by desemondo (Hermit) on Feb 12, 2010 at 10:32 UTC
    Essentially those two functions are in different name spaces
    func_name() is actually MAIN**main::func_name and mod_name::func_name lives in the mod_name name space.

    Sometimes its convenient to use a module without importing any of its subs. When you choose to do this the only way you can access that module's subs is by using the convention mod_name::func_name.

    From the Exporter module:

    How to Export:
    The arrays @EXPORT and @EXPORT_OK in a module hold lists of symbols that are going to be exported into the users name space by default, or which they can request to be exported, respectively. The symbols can represent functions, scalars, arrays, hashes, or typeglobs. The symbols must be given by full name with the exception that the ampersand in front of a function is optional, e.g.

    @EXPORT = qw(afunc $scalar @array); # afunc is a function @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc
    Selecting What To Export
    Do not export method names!
    Do not export anything else by default without a good reason!

    Exports pollute the namespace of the module user. If you must export try to use @EXPORT_OK in preference to @EXPORT and avoid short or common symbol names to reduce the risk of name clashes.

    Generally anything not exported is still accessible from outside the module using the YourModule::item_name (or $blessed_ref->method) syntax. By convention you can use a leading underscore on names to informally indicate that they are 'internal' and not for public use.

    How to Import
    In other files which wish to use your module there are three basic ways for them to load your module and import its symbols:

    use YourModule; This imports all the symbols from YourModule's @EXPORT into the namespace of the use statement.
    use YourModule (); This causes perl to load your module but does not import any symbols.
    use YourModule qw(...); This imports only the symbols listed by the caller into their namespace. All listed symbols must be in your @EXPORT or @EXPORT_OK, else an error occurs.

    ** Update:
    Ooops, thanks rovf
      func_name() is actually MAIN::func_name
      Not quite right. It is main::func_name.

      -- 
      Ronald Fischer <ynnor@mm.st>
        It is main::func_name.

        Isn't it more accurate to say "It is main::func_name in that particular case", and that in the general case it is something like __PACKAGE__::func_name, where __PACKAGE__ is the package in which the function was defined?

        >perl -wMstrict -le "package Foo; print 'in package Foo'; sub my_name { return (caller 0)[3] } print my_name(); package main; print 'in package main'; print Foo::my_name(); sub my_name { return (caller 0)[3] } print my_name(); package Foo; print 'in package Foo'; print main::my_name(); print my_name(); " in package Foo Foo::my_name in package main Foo::my_name main::my_name in package Foo main::my_name Foo::my_name