in reply to "require MIME::Base64" vs. "use..."

Try:

require MIME::Base64; import MIME::Base64; $x = encode_base64('abc');

use implies both require and import, so see if that fixes the issue.

EDIT: For those future readers, the reason why its a performance benefit in certain cases to require and import instead of use is because use is run at compile time, so no matter where you use a module, it's loaded permanently throughout the entire run of your process.

Conversely, require loads a module at runtime, and once the scope it is used in ends, the module is unloaded. Therefore, for parts of your program that are rarely called that need specific modules, it's often preferable to require them for their short lives and reduce the memory footprint of your application.

-stevieb

Replies are listed 'Best First'.
Re^2: "require MIME::Base64" vs "use..."
by tel2 (Pilgrim) on Jul 07, 2015 at 03:18 UTC
    Yes, that works thanks stevieb!

    So "require" was insufficient by itself because I was trying to access one of the functions of MIME::Base64, but that function hadn't been imported, right?

      Yes, and you could have used it anyway if you called it by its full package name:

      #!/usr/bin/env perl use strict; use warnings; use 5.010; require MIME::Base64; my $x = MIME::Base64::encode_base64('abc'); say $x;
      $ perl 1133493.pl $ YWJj $

      Note the extra new line ... you will probably want to chomp what you get back from encode_base64 ....

      Remember: Ne dederis in spiritu molere illegitimi!
        Well said, 1nickt!
        That's working for me, too.
        Thanks very much.
        chomp noted.