Domenike
Domenike

Reputation: 19

use of require in Perl

I'm trying to use require multiple times in my code, but I can to run it once.

if ($value =~ "test") {
  require "mycode1.pm";  # Execute perfect
}

if ($value =~ "code") {
    require "mycode1.pm";  # Not execute
}

if ($value =~ "last") {
  require "mycode1.pm";    # # Not execute
}

I don't understand why it doesn't run, could you please help me?

Upvotes: 0

Views: 81

Answers (1)

Dave Cross
Dave Cross

Reputation: 69224

As you've been told in the comments, Perl keeps a record of which libraries have already been loaded, so require will only be called once for each file.

A couple of notes on your code.

The right-hand operand to the binding operator should be a match (m/.../), a substitution (s/.../.../) or a transliteration (tr/.../.../). You're using strings, which are silently converted to matches at runtime - but it's better to be more explicit.

$value =~ /test/

Given that the results of your three if tests are all the same, it's probably worth converting them into a single test.

if ($value =~ /test|code|last/) {
   ...
}

Upvotes: 1

Related Questions