in reply to Custom Module question

I just wanted to mention, this is probably a better way to do it.

First, the module:

package PrintName; use strict; use warnings; use parent qw/ Exporter /; our @EXPORT_OK = qw/ print_name /; sub print_name { my($name) = @_; print "Hello, $name\n"; } 1;

The camel-casing of the package name is more of a style issue, but seems to be a fairly common practice. We're subclassing Exporter, but only exporting the function on-demand. The underscore casing of the subroutine name is purely a style issue, and there's no real community consensus of whether or not it should be applied to function names, but I tend to prefer it. We're using a lexically scoped variable for the parameter of the subroutine also, and the scope is constrained to the sub. We're not invoking the subroutine from within the module, and particularly not doing it with an undefined value.

And here's how the caller might look:

package main; use strict; use warnings; use lib "C:/Perl64/lib/printname"; use PrintName qw/ print_name /; print_name("John");

The use lib ... construct is often seen as the second part to a FindBin idiom. ...just something to keep in mind.

In the caller, we're explicitly importing print_name, and then calling it.


Dave

Replies are listed 'Best First'.
Re^2: Custom Module question
by imfaus (Initiate) on Dec 04, 2014 at 17:26 UTC
    Thanks for the comments, this is great here is my updated code
    module package Printname; use strict; use warnings; use parent qw/ Exporter /; our @EXPORT_OK = qw/ print_name /; sub print_name { my($name) = @_; print "\nHello, $name\n"; } 1;
    script
    #!/usr/bin/perl package main; use strict; use warnings; use lib "C:/Perl64/lib/printname"; use lib "C:/Perl64/site/lib/printname"; use PrintName qw/ print_name /; print_name("John");
    error C:\Scripts>perl testmodule.pl Undefined subroutine &main::print_name called at testmodule.pl line 11. line 11 is print_name("John"); I tried calling the subroutine from within the module itself but it didn't make a difference. Thanks

      Thanks for fixing your formatting.

      The other response is correct: "use PrintName", vs package Printname... these things are case sensitive.

      Also, if I recall, Windows filenames are not case-sensitive, but many other operating systems are. For all those OS's that are case-sensitive, when your module starts out with package PrintName;, the file it lives in should usually be named PrintName.pm, not printname.pm. In your environment case sensitivity of filenames is probably not an issue, but someday you'll discover the rest of the world and realize what you've been missing. ;)


      Dave

        Ok I made your changes when I run the script I receive the following error. BTW thanks again I have used perl for a while but I am learning a good deal from your help. New to modules. Thanks Again C:\Scripts>perl testmodule.pl Subroutine print_name redefined at C:/Perl64/site/lib/printname/PrintName.pm line 10. Hello, John Module
        use Printname; use strict; use warnings; use parent qw/ Exporter /; our @EXPORT_OK = qw/ print_name /; sub print_name { my($name) = @_; print "\nHello, $name\n"; } 1;
        script
        #!/usr/bin/perl package main; use strict; use warnings; use lib "C:/Perl64/lib/printname"; use lib "C:/Perl64/site/lib/printname"; # use PrintName qw/ print_name /; use PrintName; print_name("John");

      Why didn't you format it using <code>...</code> tags? I can't read it the way it is.

      Please see Writeup Formatting Tips for details. Once you've updated your post with code tags I'll look it over if I can still get to it.


      Dave

      # package Printname; package PrintName;
      poj