8 Replies - 2529 Views - Last Post: 11 May 2012 - 12:43 PM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 08:02 AM

Hi, in many places I find written that use Module is equivalent to BEGIN { require Module; Module->import; }. But I am seeing that even after using require in the is way, I still have to fully qualify the variable names from the module, same case when I use just require without the import function. If it is same either way, then what is the utility of the import function?

Is This A Good Question/Topic? 0
  • +

Replies To: Requirement of "import()" with "require" to mimic "

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 08:27 AM

I'm not sure what you mean exactly. Surely the use of the import function depends on what you define the import function to do, no?
Was This Post Helpful? 0
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 08:52 AM

No no, I didn't mean that. If the module exports a variable, say $foo, if I use that module, I can just specify $foo to get that value in my code. If I use require, I would have to use $module::foo to do the same, right? Instead of using just require, I use BEGIN { require Module; Module->import; }, I find I have to do the same too, even though it is written everywhere that this is equivalent to using use module (that is, I should be able to use just $foo, instead of requiring to write $module::foo, as in the case of using just require module). So if the import function offers no additional advantage, what is the necessity of module->import?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 09:11 AM

If you have code in which BEGIN { require Module; Module->import; } acts differently than use Module;, please show it because that shouldn't happen.

And again, the use of the import routine is to do whatever you define it to do. If you don't have an import routine, use Module won't do anything other than calling require Module.
Was This Post Helpful? 0
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 12:13 PM

Sorry, it was a typo, it's working fine. And kindly explain what do you mean by import routine. Do you mean defining which variables to be exported naturally and which on explicit demand by @EXPORT and @EXPORT_OK arrays?
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 12:16 PM

As you said yourself using use Module; is the same as requiring the module and then calling the module's import method. When I said "import routine", I was talking about that method.

Note that while the import method is most commonly defined using Exporter to export variables, you can define it yourself to do whatever you want.

This post has been edited by sepp2k: 11 May 2012 - 12:19 PM

Was This Post Helpful? 1
  • +
  • -

#7 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 12:20 PM

How can I define it myself?
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 12:39 PM

Same as you would any other method or subroutine:

sub import {
    print "This module has been loaded\n";
}



If you add that to your module file and then use use Module; in a perl file, the string "This module has been loaded" will be printed to the screen.
Was This Post Helpful? 1
  • +
  • -

#9 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Requirement of "import()" with "require" to mimic "

Posted 11 May 2012 - 12:43 PM

Thanks...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1