How can I make a Perl package for scalars?












0















I already use a Perl package (Funx.pm) for reusable code. But I'd like it to include a bunch of scalars as return values, i.e.



my $SUCCESS         =   0;
my $NOFILE = 1;
my $COPYFAIL = 2;
my $APPRUNNING = 3;
...and on and on....


Is there a way to do so and be able to just use the variables without have to import each one nor having to use Funx:: when using them?



Thanks










share|improve this question


















  • 2





    1) Can't export lexical variables. Starting by making them constants (see constant.pm). 2) Exporter allows you to specify default exports. Alternatively, it allows you to create tags the represent a group of exports. 3) Those names could easily conflict with other symbols if imported.

    – ikegami
    Nov 22 '18 at 6:04
















0















I already use a Perl package (Funx.pm) for reusable code. But I'd like it to include a bunch of scalars as return values, i.e.



my $SUCCESS         =   0;
my $NOFILE = 1;
my $COPYFAIL = 2;
my $APPRUNNING = 3;
...and on and on....


Is there a way to do so and be able to just use the variables without have to import each one nor having to use Funx:: when using them?



Thanks










share|improve this question


















  • 2





    1) Can't export lexical variables. Starting by making them constants (see constant.pm). 2) Exporter allows you to specify default exports. Alternatively, it allows you to create tags the represent a group of exports. 3) Those names could easily conflict with other symbols if imported.

    – ikegami
    Nov 22 '18 at 6:04














0












0








0








I already use a Perl package (Funx.pm) for reusable code. But I'd like it to include a bunch of scalars as return values, i.e.



my $SUCCESS         =   0;
my $NOFILE = 1;
my $COPYFAIL = 2;
my $APPRUNNING = 3;
...and on and on....


Is there a way to do so and be able to just use the variables without have to import each one nor having to use Funx:: when using them?



Thanks










share|improve this question














I already use a Perl package (Funx.pm) for reusable code. But I'd like it to include a bunch of scalars as return values, i.e.



my $SUCCESS         =   0;
my $NOFILE = 1;
my $COPYFAIL = 2;
my $APPRUNNING = 3;
...and on and on....


Is there a way to do so and be able to just use the variables without have to import each one nor having to use Funx:: when using them?



Thanks







perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 5:18









Sergio D. CaplanSergio D. Caplan

318210




318210








  • 2





    1) Can't export lexical variables. Starting by making them constants (see constant.pm). 2) Exporter allows you to specify default exports. Alternatively, it allows you to create tags the represent a group of exports. 3) Those names could easily conflict with other symbols if imported.

    – ikegami
    Nov 22 '18 at 6:04














  • 2





    1) Can't export lexical variables. Starting by making them constants (see constant.pm). 2) Exporter allows you to specify default exports. Alternatively, it allows you to create tags the represent a group of exports. 3) Those names could easily conflict with other symbols if imported.

    – ikegami
    Nov 22 '18 at 6:04








2




2





1) Can't export lexical variables. Starting by making them constants (see constant.pm). 2) Exporter allows you to specify default exports. Alternatively, it allows you to create tags the represent a group of exports. 3) Those names could easily conflict with other symbols if imported.

– ikegami
Nov 22 '18 at 6:04





1) Can't export lexical variables. Starting by making them constants (see constant.pm). 2) Exporter allows you to specify default exports. Alternatively, it allows you to create tags the represent a group of exports. 3) Those names could easily conflict with other symbols if imported.

– ikegami
Nov 22 '18 at 6:04












2 Answers
2






active

oldest

votes


















2














Prefacing this by saying that it is a bad idea, imagine if someone sets a different value to $SUCCESS anywhere in the code base. Using $Funx::SUCCESS is a much better way to go, it also provides context as to what is the success of.



our() makes a variable visible across all scopes, including across packages.



package Funx;
our($SUCCESS, $NOFILE, $COPYFAIL, $APPRUNNING);
$SUCCESS = 0;
$NOFILE = 1;
$COPYFAIL = 2;
$APPRUNNING = 3;

package main;
say($SUCCESS, $Funx::SUCCESS); # both work





share|improve this answer
























  • Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

    – Sergio D. Caplan
    Nov 22 '18 at 7:59











  • Maybe I should switch my long list of return values to constants?

    – Sergio D. Caplan
    Nov 22 '18 at 8:05






  • 3





    Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

    – Dave Cross
    Nov 22 '18 at 9:38



















1














Here's an example of a module that exports constants.



package RPi::Const;

use strict;
use warnings;

our $VERSION = '1.04';

require Exporter;
use base qw( Exporter );
our @EXPORT_OK = ();
our %EXPORT_TAGS = ( all => @EXPORT_OK );

use constant {
RPI_MODE_WPI => 0,
RPI_MODE_GPIO => 1,
RPI_MODE_GPIO_SYS => 2,
RPI_MODE_PHYS => 3,
RPI_MODE_UNINIT => -1,
};

{ # mode

my @const = qw(
RPI_MODE_WPI
RPI_MODE_GPIO
RPI_MODE_GPIO_SYS
RPI_MODE_PHYS
RPI_MODE_UNINIT
);

push @EXPORT_OK, @const;
$EXPORT_TAGS{mode} = @const;
}

use constant {
MCP23017_IODIRA => 0x00,
MCP23017_IODIRB => 0x01,
MCP23017_IPOLA => 0x02,
MCP23017_IPOLB => 0x03,
MCP23017_GPINTENA => 0x04,
MCP23017_GPINTENB => 0x05,
MCP23017_DEFVALA => 0x06,
MCP23017_DEFVALB => 0x07,
MCP23017_INTCONA => 0x08,
MCP23017_INTCONB => 0x09,
MCP23017_IOCONA => 0x0A,
MCP23017_IOCONB => 0x0B,
MCP23017_GPPUA => 0x0C,
MCP23017_GPPUB => 0x0D,
MCP23017_INTFA => 0x0E,
MCP23017_INTFB => 0x0F,
MCP23017_INTCAPA => 0x10,
MCP23017_INTCAPB => 0x11,
MCP23017_GPIOA => 0x12,
MCP23017_GPIOB => 0x13,
MCP23017_OLATA => 0x14,
MCP23017_OLATB => 0x15,

MCP23017_INPUT => 1,
MCP23017_OUTPUT => 0,
};

{ # MCP23017 GPIO Expander Registers

my @const = qw(
MCP23017_IODIRA
MCP23017_IODIRB
MCP23017_IPOLA
MCP23017_IPOLB
MCP23017_GPINTENA
MCP23017_GPINTENB
MCP23017_DEFVALA
MCP23017_DEFVALB
MCP23017_INTCONA
MCP23017_INTCONB
MCP23017_IOCONA
MCP23017_IOCONB
MCP23017_GPPUA
MCP23017_GPPUB
MCP23017_INTFA
MCP23017_INTFB
MCP23017_INTCAPA
MCP23017_INTCAPB
MCP23017_GPIOA
MCP23017_GPIOB
MCP23017_OLATA
MCP23017_OLATB
MCP23017_INPUT
MCP23017_OUTPUT
);

push @EXPORT_OK, @const;
$EXPORT_TAGS{mcp23017_registers} = @const;
}

1;
__END__


In your calling scripts, you can import each bundle (tag), or by using the :all tag, you can import all the separate groups:



use RPi::Const qw(:mcp23017_registers); # single grouping


...or



use RPi::Const qw(:all); # all groupings


Then you can use any of the imported constants:



sub mode {
my ($self, $pin, $mode) = @_;

if (! defined $mode){
my $reg = $pin > 7 ? MCP23017_IODIRB : MCP23017_IODIRA;
my $bit = _pinBit($pin);
return getRegisterBit($self->_fd, $reg, $bit);
}

_check_mode($mode);

pinMode($self->_fd, $pin, $mode);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424299%2fhow-can-i-make-a-perl-package-for-scalars%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Prefacing this by saying that it is a bad idea, imagine if someone sets a different value to $SUCCESS anywhere in the code base. Using $Funx::SUCCESS is a much better way to go, it also provides context as to what is the success of.



    our() makes a variable visible across all scopes, including across packages.



    package Funx;
    our($SUCCESS, $NOFILE, $COPYFAIL, $APPRUNNING);
    $SUCCESS = 0;
    $NOFILE = 1;
    $COPYFAIL = 2;
    $APPRUNNING = 3;

    package main;
    say($SUCCESS, $Funx::SUCCESS); # both work





    share|improve this answer
























    • Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

      – Sergio D. Caplan
      Nov 22 '18 at 7:59











    • Maybe I should switch my long list of return values to constants?

      – Sergio D. Caplan
      Nov 22 '18 at 8:05






    • 3





      Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

      – Dave Cross
      Nov 22 '18 at 9:38
















    2














    Prefacing this by saying that it is a bad idea, imagine if someone sets a different value to $SUCCESS anywhere in the code base. Using $Funx::SUCCESS is a much better way to go, it also provides context as to what is the success of.



    our() makes a variable visible across all scopes, including across packages.



    package Funx;
    our($SUCCESS, $NOFILE, $COPYFAIL, $APPRUNNING);
    $SUCCESS = 0;
    $NOFILE = 1;
    $COPYFAIL = 2;
    $APPRUNNING = 3;

    package main;
    say($SUCCESS, $Funx::SUCCESS); # both work





    share|improve this answer
























    • Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

      – Sergio D. Caplan
      Nov 22 '18 at 7:59











    • Maybe I should switch my long list of return values to constants?

      – Sergio D. Caplan
      Nov 22 '18 at 8:05






    • 3





      Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

      – Dave Cross
      Nov 22 '18 at 9:38














    2












    2








    2







    Prefacing this by saying that it is a bad idea, imagine if someone sets a different value to $SUCCESS anywhere in the code base. Using $Funx::SUCCESS is a much better way to go, it also provides context as to what is the success of.



    our() makes a variable visible across all scopes, including across packages.



    package Funx;
    our($SUCCESS, $NOFILE, $COPYFAIL, $APPRUNNING);
    $SUCCESS = 0;
    $NOFILE = 1;
    $COPYFAIL = 2;
    $APPRUNNING = 3;

    package main;
    say($SUCCESS, $Funx::SUCCESS); # both work





    share|improve this answer













    Prefacing this by saying that it is a bad idea, imagine if someone sets a different value to $SUCCESS anywhere in the code base. Using $Funx::SUCCESS is a much better way to go, it also provides context as to what is the success of.



    our() makes a variable visible across all scopes, including across packages.



    package Funx;
    our($SUCCESS, $NOFILE, $COPYFAIL, $APPRUNNING);
    $SUCCESS = 0;
    $NOFILE = 1;
    $COPYFAIL = 2;
    $APPRUNNING = 3;

    package main;
    say($SUCCESS, $Funx::SUCCESS); # both work






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 6:29









    lodlod

    953612




    953612













    • Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

      – Sergio D. Caplan
      Nov 22 '18 at 7:59











    • Maybe I should switch my long list of return values to constants?

      – Sergio D. Caplan
      Nov 22 '18 at 8:05






    • 3





      Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

      – Dave Cross
      Nov 22 '18 at 9:38



















    • Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

      – Sergio D. Caplan
      Nov 22 '18 at 7:59











    • Maybe I should switch my long list of return values to constants?

      – Sergio D. Caplan
      Nov 22 '18 at 8:05






    • 3





      Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

      – Dave Cross
      Nov 22 '18 at 9:38

















    Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

    – Sergio D. Caplan
    Nov 22 '18 at 7:59





    Thanks. For now I can live with the risk of reassignment because I'm the only one using my code.

    – Sergio D. Caplan
    Nov 22 '18 at 7:59













    Maybe I should switch my long list of return values to constants?

    – Sergio D. Caplan
    Nov 22 '18 at 8:05





    Maybe I should switch my long list of return values to constants?

    – Sergio D. Caplan
    Nov 22 '18 at 8:05




    3




    3





    Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

    – Dave Cross
    Nov 22 '18 at 9:38





    Your say($SUCCESS) in the main package only works because our has a lexical effect and package doesn't create a new lexical scope. If (as would be more normal) package Funx and package main were in different files, then his code wouldn't work. You should probably add something about using Exporter to work round that.

    – Dave Cross
    Nov 22 '18 at 9:38













    1














    Here's an example of a module that exports constants.



    package RPi::Const;

    use strict;
    use warnings;

    our $VERSION = '1.04';

    require Exporter;
    use base qw( Exporter );
    our @EXPORT_OK = ();
    our %EXPORT_TAGS = ( all => @EXPORT_OK );

    use constant {
    RPI_MODE_WPI => 0,
    RPI_MODE_GPIO => 1,
    RPI_MODE_GPIO_SYS => 2,
    RPI_MODE_PHYS => 3,
    RPI_MODE_UNINIT => -1,
    };

    { # mode

    my @const = qw(
    RPI_MODE_WPI
    RPI_MODE_GPIO
    RPI_MODE_GPIO_SYS
    RPI_MODE_PHYS
    RPI_MODE_UNINIT
    );

    push @EXPORT_OK, @const;
    $EXPORT_TAGS{mode} = @const;
    }

    use constant {
    MCP23017_IODIRA => 0x00,
    MCP23017_IODIRB => 0x01,
    MCP23017_IPOLA => 0x02,
    MCP23017_IPOLB => 0x03,
    MCP23017_GPINTENA => 0x04,
    MCP23017_GPINTENB => 0x05,
    MCP23017_DEFVALA => 0x06,
    MCP23017_DEFVALB => 0x07,
    MCP23017_INTCONA => 0x08,
    MCP23017_INTCONB => 0x09,
    MCP23017_IOCONA => 0x0A,
    MCP23017_IOCONB => 0x0B,
    MCP23017_GPPUA => 0x0C,
    MCP23017_GPPUB => 0x0D,
    MCP23017_INTFA => 0x0E,
    MCP23017_INTFB => 0x0F,
    MCP23017_INTCAPA => 0x10,
    MCP23017_INTCAPB => 0x11,
    MCP23017_GPIOA => 0x12,
    MCP23017_GPIOB => 0x13,
    MCP23017_OLATA => 0x14,
    MCP23017_OLATB => 0x15,

    MCP23017_INPUT => 1,
    MCP23017_OUTPUT => 0,
    };

    { # MCP23017 GPIO Expander Registers

    my @const = qw(
    MCP23017_IODIRA
    MCP23017_IODIRB
    MCP23017_IPOLA
    MCP23017_IPOLB
    MCP23017_GPINTENA
    MCP23017_GPINTENB
    MCP23017_DEFVALA
    MCP23017_DEFVALB
    MCP23017_INTCONA
    MCP23017_INTCONB
    MCP23017_IOCONA
    MCP23017_IOCONB
    MCP23017_GPPUA
    MCP23017_GPPUB
    MCP23017_INTFA
    MCP23017_INTFB
    MCP23017_INTCAPA
    MCP23017_INTCAPB
    MCP23017_GPIOA
    MCP23017_GPIOB
    MCP23017_OLATA
    MCP23017_OLATB
    MCP23017_INPUT
    MCP23017_OUTPUT
    );

    push @EXPORT_OK, @const;
    $EXPORT_TAGS{mcp23017_registers} = @const;
    }

    1;
    __END__


    In your calling scripts, you can import each bundle (tag), or by using the :all tag, you can import all the separate groups:



    use RPi::Const qw(:mcp23017_registers); # single grouping


    ...or



    use RPi::Const qw(:all); # all groupings


    Then you can use any of the imported constants:



    sub mode {
    my ($self, $pin, $mode) = @_;

    if (! defined $mode){
    my $reg = $pin > 7 ? MCP23017_IODIRB : MCP23017_IODIRA;
    my $bit = _pinBit($pin);
    return getRegisterBit($self->_fd, $reg, $bit);
    }

    _check_mode($mode);

    pinMode($self->_fd, $pin, $mode);
    }





    share|improve this answer




























      1














      Here's an example of a module that exports constants.



      package RPi::Const;

      use strict;
      use warnings;

      our $VERSION = '1.04';

      require Exporter;
      use base qw( Exporter );
      our @EXPORT_OK = ();
      our %EXPORT_TAGS = ( all => @EXPORT_OK );

      use constant {
      RPI_MODE_WPI => 0,
      RPI_MODE_GPIO => 1,
      RPI_MODE_GPIO_SYS => 2,
      RPI_MODE_PHYS => 3,
      RPI_MODE_UNINIT => -1,
      };

      { # mode

      my @const = qw(
      RPI_MODE_WPI
      RPI_MODE_GPIO
      RPI_MODE_GPIO_SYS
      RPI_MODE_PHYS
      RPI_MODE_UNINIT
      );

      push @EXPORT_OK, @const;
      $EXPORT_TAGS{mode} = @const;
      }

      use constant {
      MCP23017_IODIRA => 0x00,
      MCP23017_IODIRB => 0x01,
      MCP23017_IPOLA => 0x02,
      MCP23017_IPOLB => 0x03,
      MCP23017_GPINTENA => 0x04,
      MCP23017_GPINTENB => 0x05,
      MCP23017_DEFVALA => 0x06,
      MCP23017_DEFVALB => 0x07,
      MCP23017_INTCONA => 0x08,
      MCP23017_INTCONB => 0x09,
      MCP23017_IOCONA => 0x0A,
      MCP23017_IOCONB => 0x0B,
      MCP23017_GPPUA => 0x0C,
      MCP23017_GPPUB => 0x0D,
      MCP23017_INTFA => 0x0E,
      MCP23017_INTFB => 0x0F,
      MCP23017_INTCAPA => 0x10,
      MCP23017_INTCAPB => 0x11,
      MCP23017_GPIOA => 0x12,
      MCP23017_GPIOB => 0x13,
      MCP23017_OLATA => 0x14,
      MCP23017_OLATB => 0x15,

      MCP23017_INPUT => 1,
      MCP23017_OUTPUT => 0,
      };

      { # MCP23017 GPIO Expander Registers

      my @const = qw(
      MCP23017_IODIRA
      MCP23017_IODIRB
      MCP23017_IPOLA
      MCP23017_IPOLB
      MCP23017_GPINTENA
      MCP23017_GPINTENB
      MCP23017_DEFVALA
      MCP23017_DEFVALB
      MCP23017_INTCONA
      MCP23017_INTCONB
      MCP23017_IOCONA
      MCP23017_IOCONB
      MCP23017_GPPUA
      MCP23017_GPPUB
      MCP23017_INTFA
      MCP23017_INTFB
      MCP23017_INTCAPA
      MCP23017_INTCAPB
      MCP23017_GPIOA
      MCP23017_GPIOB
      MCP23017_OLATA
      MCP23017_OLATB
      MCP23017_INPUT
      MCP23017_OUTPUT
      );

      push @EXPORT_OK, @const;
      $EXPORT_TAGS{mcp23017_registers} = @const;
      }

      1;
      __END__


      In your calling scripts, you can import each bundle (tag), or by using the :all tag, you can import all the separate groups:



      use RPi::Const qw(:mcp23017_registers); # single grouping


      ...or



      use RPi::Const qw(:all); # all groupings


      Then you can use any of the imported constants:



      sub mode {
      my ($self, $pin, $mode) = @_;

      if (! defined $mode){
      my $reg = $pin > 7 ? MCP23017_IODIRB : MCP23017_IODIRA;
      my $bit = _pinBit($pin);
      return getRegisterBit($self->_fd, $reg, $bit);
      }

      _check_mode($mode);

      pinMode($self->_fd, $pin, $mode);
      }





      share|improve this answer


























        1












        1








        1







        Here's an example of a module that exports constants.



        package RPi::Const;

        use strict;
        use warnings;

        our $VERSION = '1.04';

        require Exporter;
        use base qw( Exporter );
        our @EXPORT_OK = ();
        our %EXPORT_TAGS = ( all => @EXPORT_OK );

        use constant {
        RPI_MODE_WPI => 0,
        RPI_MODE_GPIO => 1,
        RPI_MODE_GPIO_SYS => 2,
        RPI_MODE_PHYS => 3,
        RPI_MODE_UNINIT => -1,
        };

        { # mode

        my @const = qw(
        RPI_MODE_WPI
        RPI_MODE_GPIO
        RPI_MODE_GPIO_SYS
        RPI_MODE_PHYS
        RPI_MODE_UNINIT
        );

        push @EXPORT_OK, @const;
        $EXPORT_TAGS{mode} = @const;
        }

        use constant {
        MCP23017_IODIRA => 0x00,
        MCP23017_IODIRB => 0x01,
        MCP23017_IPOLA => 0x02,
        MCP23017_IPOLB => 0x03,
        MCP23017_GPINTENA => 0x04,
        MCP23017_GPINTENB => 0x05,
        MCP23017_DEFVALA => 0x06,
        MCP23017_DEFVALB => 0x07,
        MCP23017_INTCONA => 0x08,
        MCP23017_INTCONB => 0x09,
        MCP23017_IOCONA => 0x0A,
        MCP23017_IOCONB => 0x0B,
        MCP23017_GPPUA => 0x0C,
        MCP23017_GPPUB => 0x0D,
        MCP23017_INTFA => 0x0E,
        MCP23017_INTFB => 0x0F,
        MCP23017_INTCAPA => 0x10,
        MCP23017_INTCAPB => 0x11,
        MCP23017_GPIOA => 0x12,
        MCP23017_GPIOB => 0x13,
        MCP23017_OLATA => 0x14,
        MCP23017_OLATB => 0x15,

        MCP23017_INPUT => 1,
        MCP23017_OUTPUT => 0,
        };

        { # MCP23017 GPIO Expander Registers

        my @const = qw(
        MCP23017_IODIRA
        MCP23017_IODIRB
        MCP23017_IPOLA
        MCP23017_IPOLB
        MCP23017_GPINTENA
        MCP23017_GPINTENB
        MCP23017_DEFVALA
        MCP23017_DEFVALB
        MCP23017_INTCONA
        MCP23017_INTCONB
        MCP23017_IOCONA
        MCP23017_IOCONB
        MCP23017_GPPUA
        MCP23017_GPPUB
        MCP23017_INTFA
        MCP23017_INTFB
        MCP23017_INTCAPA
        MCP23017_INTCAPB
        MCP23017_GPIOA
        MCP23017_GPIOB
        MCP23017_OLATA
        MCP23017_OLATB
        MCP23017_INPUT
        MCP23017_OUTPUT
        );

        push @EXPORT_OK, @const;
        $EXPORT_TAGS{mcp23017_registers} = @const;
        }

        1;
        __END__


        In your calling scripts, you can import each bundle (tag), or by using the :all tag, you can import all the separate groups:



        use RPi::Const qw(:mcp23017_registers); # single grouping


        ...or



        use RPi::Const qw(:all); # all groupings


        Then you can use any of the imported constants:



        sub mode {
        my ($self, $pin, $mode) = @_;

        if (! defined $mode){
        my $reg = $pin > 7 ? MCP23017_IODIRB : MCP23017_IODIRA;
        my $bit = _pinBit($pin);
        return getRegisterBit($self->_fd, $reg, $bit);
        }

        _check_mode($mode);

        pinMode($self->_fd, $pin, $mode);
        }





        share|improve this answer













        Here's an example of a module that exports constants.



        package RPi::Const;

        use strict;
        use warnings;

        our $VERSION = '1.04';

        require Exporter;
        use base qw( Exporter );
        our @EXPORT_OK = ();
        our %EXPORT_TAGS = ( all => @EXPORT_OK );

        use constant {
        RPI_MODE_WPI => 0,
        RPI_MODE_GPIO => 1,
        RPI_MODE_GPIO_SYS => 2,
        RPI_MODE_PHYS => 3,
        RPI_MODE_UNINIT => -1,
        };

        { # mode

        my @const = qw(
        RPI_MODE_WPI
        RPI_MODE_GPIO
        RPI_MODE_GPIO_SYS
        RPI_MODE_PHYS
        RPI_MODE_UNINIT
        );

        push @EXPORT_OK, @const;
        $EXPORT_TAGS{mode} = @const;
        }

        use constant {
        MCP23017_IODIRA => 0x00,
        MCP23017_IODIRB => 0x01,
        MCP23017_IPOLA => 0x02,
        MCP23017_IPOLB => 0x03,
        MCP23017_GPINTENA => 0x04,
        MCP23017_GPINTENB => 0x05,
        MCP23017_DEFVALA => 0x06,
        MCP23017_DEFVALB => 0x07,
        MCP23017_INTCONA => 0x08,
        MCP23017_INTCONB => 0x09,
        MCP23017_IOCONA => 0x0A,
        MCP23017_IOCONB => 0x0B,
        MCP23017_GPPUA => 0x0C,
        MCP23017_GPPUB => 0x0D,
        MCP23017_INTFA => 0x0E,
        MCP23017_INTFB => 0x0F,
        MCP23017_INTCAPA => 0x10,
        MCP23017_INTCAPB => 0x11,
        MCP23017_GPIOA => 0x12,
        MCP23017_GPIOB => 0x13,
        MCP23017_OLATA => 0x14,
        MCP23017_OLATB => 0x15,

        MCP23017_INPUT => 1,
        MCP23017_OUTPUT => 0,
        };

        { # MCP23017 GPIO Expander Registers

        my @const = qw(
        MCP23017_IODIRA
        MCP23017_IODIRB
        MCP23017_IPOLA
        MCP23017_IPOLB
        MCP23017_GPINTENA
        MCP23017_GPINTENB
        MCP23017_DEFVALA
        MCP23017_DEFVALB
        MCP23017_INTCONA
        MCP23017_INTCONB
        MCP23017_IOCONA
        MCP23017_IOCONB
        MCP23017_GPPUA
        MCP23017_GPPUB
        MCP23017_INTFA
        MCP23017_INTFB
        MCP23017_INTCAPA
        MCP23017_INTCAPB
        MCP23017_GPIOA
        MCP23017_GPIOB
        MCP23017_OLATA
        MCP23017_OLATB
        MCP23017_INPUT
        MCP23017_OUTPUT
        );

        push @EXPORT_OK, @const;
        $EXPORT_TAGS{mcp23017_registers} = @const;
        }

        1;
        __END__


        In your calling scripts, you can import each bundle (tag), or by using the :all tag, you can import all the separate groups:



        use RPi::Const qw(:mcp23017_registers); # single grouping


        ...or



        use RPi::Const qw(:all); # all groupings


        Then you can use any of the imported constants:



        sub mode {
        my ($self, $pin, $mode) = @_;

        if (! defined $mode){
        my $reg = $pin > 7 ? MCP23017_IODIRB : MCP23017_IODIRA;
        my $bit = _pinBit($pin);
        return getRegisterBit($self->_fd, $reg, $bit);
        }

        _check_mode($mode);

        pinMode($self->_fd, $pin, $mode);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 15:52









        steviebstevieb

        7,23731933




        7,23731933






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424299%2fhow-can-i-make-a-perl-package-for-scalars%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy