Can I condense my export routine in my Perl package?
up vote
1
down vote
favorite
My current Perl package export feels long (snippet below). And yes, it's better to type all that just once in one place so my many Perl scripts can accesss it all with just:
use Funx;
Still I was just hoping there would be an easy way to export everything with less typing.
package Funx;
#use strict;
use warnings;
use DBI;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(pdone dbstart dbstop dbc dbcdata numnums $SUCCESS $NOFILE
$COPYFAIL $SOXFAIL $CURLFAIL $OPENFAIL $APPRUNNING $RAWDBEXIISTS $DBCREATEERR $DBCONNECTERR $TMPFILEERR $DBWRITEERR $INVALIDUSER $DBLOCKERR $DBUNLOCKERR WERR);
our $SUCCESS = 0;
our $NOFILE = 1;
our $COPYFAIL = 2;
our $SOXFAIL = 3;
our $CURLFAIL = 4;
our $OPENFAIL = 5;
our $APPRUNNING = 6;
our $RAWDBEXIISTS = 7;
our $DBCREATEERR = 8;
our $DBCONNECTERR = 9;
our $TMPFILEERR = 10;
our $DBWRITEERR = 11;
our $INVALIDUSER = 12;
our $DBLOCKERR = 13;
our $DBUNLOCKERR = 14;
use constant WERR => 100;
perl
add a comment |
up vote
1
down vote
favorite
My current Perl package export feels long (snippet below). And yes, it's better to type all that just once in one place so my many Perl scripts can accesss it all with just:
use Funx;
Still I was just hoping there would be an easy way to export everything with less typing.
package Funx;
#use strict;
use warnings;
use DBI;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(pdone dbstart dbstop dbc dbcdata numnums $SUCCESS $NOFILE
$COPYFAIL $SOXFAIL $CURLFAIL $OPENFAIL $APPRUNNING $RAWDBEXIISTS $DBCREATEERR $DBCONNECTERR $TMPFILEERR $DBWRITEERR $INVALIDUSER $DBLOCKERR $DBUNLOCKERR WERR);
our $SUCCESS = 0;
our $NOFILE = 1;
our $COPYFAIL = 2;
our $SOXFAIL = 3;
our $CURLFAIL = 4;
our $OPENFAIL = 5;
our $APPRUNNING = 6;
our $RAWDBEXIISTS = 7;
our $DBCREATEERR = 8;
our $DBCONNECTERR = 9;
our $TMPFILEERR = 10;
our $DBWRITEERR = 11;
our $INVALIDUSER = 12;
our $DBLOCKERR = 13;
our $DBUNLOCKERR = 14;
use constant WERR => 100;
perl
What does Funx stand for? It's a good idea to use descriptive names for things. Also, why isuse strict
commented out?
– simbabque
Nov 22 at 15:35
1
Export a function that returns a hash? Or just export a hash directly?
– stevieb
Nov 22 at 15:44
1
My 2 cents: call the packageErr
, and don't export. Then you can usereturn $Err::OPENFAIL;
which is, IMO, clearer anyway.
– Tanktalus
Nov 22 at 15:46
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My current Perl package export feels long (snippet below). And yes, it's better to type all that just once in one place so my many Perl scripts can accesss it all with just:
use Funx;
Still I was just hoping there would be an easy way to export everything with less typing.
package Funx;
#use strict;
use warnings;
use DBI;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(pdone dbstart dbstop dbc dbcdata numnums $SUCCESS $NOFILE
$COPYFAIL $SOXFAIL $CURLFAIL $OPENFAIL $APPRUNNING $RAWDBEXIISTS $DBCREATEERR $DBCONNECTERR $TMPFILEERR $DBWRITEERR $INVALIDUSER $DBLOCKERR $DBUNLOCKERR WERR);
our $SUCCESS = 0;
our $NOFILE = 1;
our $COPYFAIL = 2;
our $SOXFAIL = 3;
our $CURLFAIL = 4;
our $OPENFAIL = 5;
our $APPRUNNING = 6;
our $RAWDBEXIISTS = 7;
our $DBCREATEERR = 8;
our $DBCONNECTERR = 9;
our $TMPFILEERR = 10;
our $DBWRITEERR = 11;
our $INVALIDUSER = 12;
our $DBLOCKERR = 13;
our $DBUNLOCKERR = 14;
use constant WERR => 100;
perl
My current Perl package export feels long (snippet below). And yes, it's better to type all that just once in one place so my many Perl scripts can accesss it all with just:
use Funx;
Still I was just hoping there would be an easy way to export everything with less typing.
package Funx;
#use strict;
use warnings;
use DBI;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(pdone dbstart dbstop dbc dbcdata numnums $SUCCESS $NOFILE
$COPYFAIL $SOXFAIL $CURLFAIL $OPENFAIL $APPRUNNING $RAWDBEXIISTS $DBCREATEERR $DBCONNECTERR $TMPFILEERR $DBWRITEERR $INVALIDUSER $DBLOCKERR $DBUNLOCKERR WERR);
our $SUCCESS = 0;
our $NOFILE = 1;
our $COPYFAIL = 2;
our $SOXFAIL = 3;
our $CURLFAIL = 4;
our $OPENFAIL = 5;
our $APPRUNNING = 6;
our $RAWDBEXIISTS = 7;
our $DBCREATEERR = 8;
our $DBCONNECTERR = 9;
our $TMPFILEERR = 10;
our $DBWRITEERR = 11;
our $INVALIDUSER = 12;
our $DBLOCKERR = 13;
our $DBUNLOCKERR = 14;
use constant WERR => 100;
perl
perl
edited Nov 22 at 15:34
simbabque
43.6k754101
43.6k754101
asked Nov 22 at 15:33
Sergio D. Caplan
176110
176110
What does Funx stand for? It's a good idea to use descriptive names for things. Also, why isuse strict
commented out?
– simbabque
Nov 22 at 15:35
1
Export a function that returns a hash? Or just export a hash directly?
– stevieb
Nov 22 at 15:44
1
My 2 cents: call the packageErr
, and don't export. Then you can usereturn $Err::OPENFAIL;
which is, IMO, clearer anyway.
– Tanktalus
Nov 22 at 15:46
add a comment |
What does Funx stand for? It's a good idea to use descriptive names for things. Also, why isuse strict
commented out?
– simbabque
Nov 22 at 15:35
1
Export a function that returns a hash? Or just export a hash directly?
– stevieb
Nov 22 at 15:44
1
My 2 cents: call the packageErr
, and don't export. Then you can usereturn $Err::OPENFAIL;
which is, IMO, clearer anyway.
– Tanktalus
Nov 22 at 15:46
What does Funx stand for? It's a good idea to use descriptive names for things. Also, why is
use strict
commented out?– simbabque
Nov 22 at 15:35
What does Funx stand for? It's a good idea to use descriptive names for things. Also, why is
use strict
commented out?– simbabque
Nov 22 at 15:35
1
1
Export a function that returns a hash? Or just export a hash directly?
– stevieb
Nov 22 at 15:44
Export a function that returns a hash? Or just export a hash directly?
– stevieb
Nov 22 at 15:44
1
1
My 2 cents: call the package
Err
, and don't export. Then you can use return $Err::OPENFAIL;
which is, IMO, clearer anyway.– Tanktalus
Nov 22 at 15:46
My 2 cents: call the package
Err
, and don't export. Then you can use return $Err::OPENFAIL;
which is, IMO, clearer anyway.– Tanktalus
Nov 22 at 15:46
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
If you used constants instead of variables,
package Funx;
use strict;
use warnings;
use constant qw( );
use Exporter qw( import );
BEGIN {
my %error_codes = (
FUNX_SUCCESS => 0,
FUNX_NOFILE => 1,
FUNX_COPYFAIL => 2,
FUNX_SOXFAIL => 3,
FUNX_CURLFAIL => 4,
FUNX_OPENFAIL => 5,
FUNX_APPRUNNING => 6,
FUNX_RAWDBEXIISTS => 7,
FUNX_DBCREATEERR => 8,
FUNX_DBCONNECTERR => 9,
FUNX_TMPFILEERR => 10,
FUNX_DBWRITEERR => 11,
FUNX_INVALIDUSER => 12,
FUNX_DBLOCKERR => 13,
FUNX_DBUNLOCKERR => 14,
FUNX_WERR => 100,
);
constant->import(%error_codes);
my @syms = keys(%error_codes);
our @EXPORT_OK = @syms;
our %EXPORT_TAGS = ( ALL => @syms, ERROR_CODES => @syms );
}
On top of addressing the issue you raised, the above
- Fixes the polluting of the user's namespace. Don't dump a bunch of symbols into other namespaces by default!
- Fixes the poor names that could conflict with other modules. You think you're the only module that has a code for
SUCCESS
? - Fixes the polluting of your module's
@ISA
. Funx is not a subclass of Exporter.
Usage:
use Funx; # Imports nothing.
use Funx qw( ); # Imports nothing.
use Funx qw( :ERROR_CODES ); # Imports error codes.
use Funx qw( :ALL ); # Imports error codes.
use Funx qw( FUNX_SUCCESS FUNX_NOFILE ); # Imports specific error codes.
add a comment |
up vote
1
down vote
You can use source filter to add some custom preprocessing.
Example:
package MyExport;
use Filter::Util::Call;
sub import {
my ($type) = @_;
my ($ref) = ;
filter_add(bless $ref);
}
sub filter {
my ($self) = @_;
my ($status);
if (($status = filter_read()) > 0) {
s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
}
$status;
}
1;
Usage:
...
use MyExport;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT;
...
our export $SUCCESS = 0;
...
Note that this implementation may be buggy. Basically s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
regexp turns lines like
our export $SUCCESS = 0;
into
push @EXPORT, '$SUCCESS'; our $SUCCESS = 0;
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
If you used constants instead of variables,
package Funx;
use strict;
use warnings;
use constant qw( );
use Exporter qw( import );
BEGIN {
my %error_codes = (
FUNX_SUCCESS => 0,
FUNX_NOFILE => 1,
FUNX_COPYFAIL => 2,
FUNX_SOXFAIL => 3,
FUNX_CURLFAIL => 4,
FUNX_OPENFAIL => 5,
FUNX_APPRUNNING => 6,
FUNX_RAWDBEXIISTS => 7,
FUNX_DBCREATEERR => 8,
FUNX_DBCONNECTERR => 9,
FUNX_TMPFILEERR => 10,
FUNX_DBWRITEERR => 11,
FUNX_INVALIDUSER => 12,
FUNX_DBLOCKERR => 13,
FUNX_DBUNLOCKERR => 14,
FUNX_WERR => 100,
);
constant->import(%error_codes);
my @syms = keys(%error_codes);
our @EXPORT_OK = @syms;
our %EXPORT_TAGS = ( ALL => @syms, ERROR_CODES => @syms );
}
On top of addressing the issue you raised, the above
- Fixes the polluting of the user's namespace. Don't dump a bunch of symbols into other namespaces by default!
- Fixes the poor names that could conflict with other modules. You think you're the only module that has a code for
SUCCESS
? - Fixes the polluting of your module's
@ISA
. Funx is not a subclass of Exporter.
Usage:
use Funx; # Imports nothing.
use Funx qw( ); # Imports nothing.
use Funx qw( :ERROR_CODES ); # Imports error codes.
use Funx qw( :ALL ); # Imports error codes.
use Funx qw( FUNX_SUCCESS FUNX_NOFILE ); # Imports specific error codes.
add a comment |
up vote
5
down vote
If you used constants instead of variables,
package Funx;
use strict;
use warnings;
use constant qw( );
use Exporter qw( import );
BEGIN {
my %error_codes = (
FUNX_SUCCESS => 0,
FUNX_NOFILE => 1,
FUNX_COPYFAIL => 2,
FUNX_SOXFAIL => 3,
FUNX_CURLFAIL => 4,
FUNX_OPENFAIL => 5,
FUNX_APPRUNNING => 6,
FUNX_RAWDBEXIISTS => 7,
FUNX_DBCREATEERR => 8,
FUNX_DBCONNECTERR => 9,
FUNX_TMPFILEERR => 10,
FUNX_DBWRITEERR => 11,
FUNX_INVALIDUSER => 12,
FUNX_DBLOCKERR => 13,
FUNX_DBUNLOCKERR => 14,
FUNX_WERR => 100,
);
constant->import(%error_codes);
my @syms = keys(%error_codes);
our @EXPORT_OK = @syms;
our %EXPORT_TAGS = ( ALL => @syms, ERROR_CODES => @syms );
}
On top of addressing the issue you raised, the above
- Fixes the polluting of the user's namespace. Don't dump a bunch of symbols into other namespaces by default!
- Fixes the poor names that could conflict with other modules. You think you're the only module that has a code for
SUCCESS
? - Fixes the polluting of your module's
@ISA
. Funx is not a subclass of Exporter.
Usage:
use Funx; # Imports nothing.
use Funx qw( ); # Imports nothing.
use Funx qw( :ERROR_CODES ); # Imports error codes.
use Funx qw( :ALL ); # Imports error codes.
use Funx qw( FUNX_SUCCESS FUNX_NOFILE ); # Imports specific error codes.
add a comment |
up vote
5
down vote
up vote
5
down vote
If you used constants instead of variables,
package Funx;
use strict;
use warnings;
use constant qw( );
use Exporter qw( import );
BEGIN {
my %error_codes = (
FUNX_SUCCESS => 0,
FUNX_NOFILE => 1,
FUNX_COPYFAIL => 2,
FUNX_SOXFAIL => 3,
FUNX_CURLFAIL => 4,
FUNX_OPENFAIL => 5,
FUNX_APPRUNNING => 6,
FUNX_RAWDBEXIISTS => 7,
FUNX_DBCREATEERR => 8,
FUNX_DBCONNECTERR => 9,
FUNX_TMPFILEERR => 10,
FUNX_DBWRITEERR => 11,
FUNX_INVALIDUSER => 12,
FUNX_DBLOCKERR => 13,
FUNX_DBUNLOCKERR => 14,
FUNX_WERR => 100,
);
constant->import(%error_codes);
my @syms = keys(%error_codes);
our @EXPORT_OK = @syms;
our %EXPORT_TAGS = ( ALL => @syms, ERROR_CODES => @syms );
}
On top of addressing the issue you raised, the above
- Fixes the polluting of the user's namespace. Don't dump a bunch of symbols into other namespaces by default!
- Fixes the poor names that could conflict with other modules. You think you're the only module that has a code for
SUCCESS
? - Fixes the polluting of your module's
@ISA
. Funx is not a subclass of Exporter.
Usage:
use Funx; # Imports nothing.
use Funx qw( ); # Imports nothing.
use Funx qw( :ERROR_CODES ); # Imports error codes.
use Funx qw( :ALL ); # Imports error codes.
use Funx qw( FUNX_SUCCESS FUNX_NOFILE ); # Imports specific error codes.
If you used constants instead of variables,
package Funx;
use strict;
use warnings;
use constant qw( );
use Exporter qw( import );
BEGIN {
my %error_codes = (
FUNX_SUCCESS => 0,
FUNX_NOFILE => 1,
FUNX_COPYFAIL => 2,
FUNX_SOXFAIL => 3,
FUNX_CURLFAIL => 4,
FUNX_OPENFAIL => 5,
FUNX_APPRUNNING => 6,
FUNX_RAWDBEXIISTS => 7,
FUNX_DBCREATEERR => 8,
FUNX_DBCONNECTERR => 9,
FUNX_TMPFILEERR => 10,
FUNX_DBWRITEERR => 11,
FUNX_INVALIDUSER => 12,
FUNX_DBLOCKERR => 13,
FUNX_DBUNLOCKERR => 14,
FUNX_WERR => 100,
);
constant->import(%error_codes);
my @syms = keys(%error_codes);
our @EXPORT_OK = @syms;
our %EXPORT_TAGS = ( ALL => @syms, ERROR_CODES => @syms );
}
On top of addressing the issue you raised, the above
- Fixes the polluting of the user's namespace. Don't dump a bunch of symbols into other namespaces by default!
- Fixes the poor names that could conflict with other modules. You think you're the only module that has a code for
SUCCESS
? - Fixes the polluting of your module's
@ISA
. Funx is not a subclass of Exporter.
Usage:
use Funx; # Imports nothing.
use Funx qw( ); # Imports nothing.
use Funx qw( :ERROR_CODES ); # Imports error codes.
use Funx qw( :ALL ); # Imports error codes.
use Funx qw( FUNX_SUCCESS FUNX_NOFILE ); # Imports specific error codes.
edited Nov 23 at 5:52
answered Nov 23 at 5:42
ikegami
260k11174394
260k11174394
add a comment |
add a comment |
up vote
1
down vote
You can use source filter to add some custom preprocessing.
Example:
package MyExport;
use Filter::Util::Call;
sub import {
my ($type) = @_;
my ($ref) = ;
filter_add(bless $ref);
}
sub filter {
my ($self) = @_;
my ($status);
if (($status = filter_read()) > 0) {
s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
}
$status;
}
1;
Usage:
...
use MyExport;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT;
...
our export $SUCCESS = 0;
...
Note that this implementation may be buggy. Basically s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
regexp turns lines like
our export $SUCCESS = 0;
into
push @EXPORT, '$SUCCESS'; our $SUCCESS = 0;
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
add a comment |
up vote
1
down vote
You can use source filter to add some custom preprocessing.
Example:
package MyExport;
use Filter::Util::Call;
sub import {
my ($type) = @_;
my ($ref) = ;
filter_add(bless $ref);
}
sub filter {
my ($self) = @_;
my ($status);
if (($status = filter_read()) > 0) {
s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
}
$status;
}
1;
Usage:
...
use MyExport;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT;
...
our export $SUCCESS = 0;
...
Note that this implementation may be buggy. Basically s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
regexp turns lines like
our export $SUCCESS = 0;
into
push @EXPORT, '$SUCCESS'; our $SUCCESS = 0;
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use source filter to add some custom preprocessing.
Example:
package MyExport;
use Filter::Util::Call;
sub import {
my ($type) = @_;
my ($ref) = ;
filter_add(bless $ref);
}
sub filter {
my ($self) = @_;
my ($status);
if (($status = filter_read()) > 0) {
s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
}
$status;
}
1;
Usage:
...
use MyExport;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT;
...
our export $SUCCESS = 0;
...
Note that this implementation may be buggy. Basically s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
regexp turns lines like
our export $SUCCESS = 0;
into
push @EXPORT, '$SUCCESS'; our $SUCCESS = 0;
You can use source filter to add some custom preprocessing.
Example:
package MyExport;
use Filter::Util::Call;
sub import {
my ($type) = @_;
my ($ref) = ;
filter_add(bless $ref);
}
sub filter {
my ($self) = @_;
my ($status);
if (($status = filter_read()) > 0) {
s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
}
$status;
}
1;
Usage:
...
use MyExport;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT;
...
our export $SUCCESS = 0;
...
Note that this implementation may be buggy. Basically s/^(.*)s+exports+(S+)(.*)$/push @EXPORT, '$2'; $1 $2 $3/;
regexp turns lines like
our export $SUCCESS = 0;
into
push @EXPORT, '$SUCCESS'; our $SUCCESS = 0;
answered Nov 22 at 17:03
Ujin
61910
61910
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
add a comment |
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
Please don't seriously suggest source filters for anything more than trivial oneliners...
– Grinnz
Nov 23 at 19:58
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434203%2fcan-i-condense-my-export-routine-in-my-perl-package%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What does Funx stand for? It's a good idea to use descriptive names for things. Also, why is
use strict
commented out?– simbabque
Nov 22 at 15:35
1
Export a function that returns a hash? Or just export a hash directly?
– stevieb
Nov 22 at 15:44
1
My 2 cents: call the package
Err
, and don't export. Then you can usereturn $Err::OPENFAIL;
which is, IMO, clearer anyway.– Tanktalus
Nov 22 at 15:46