Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Header file management

by smaines (Novice)
on Apr 30, 2018 at 09:10 UTC ( [id://1213800]=perlquestion: print w/replies, xml ) Need Help??

smaines has asked for the wisdom of the Perl Monks concerning the following question:

Most exalted Perl Monks,

I have sought my answer in your pages without success, perhaps for want of the proper keywords. Apologies if my humble query has been addressed already.

I have several modules which begin,

package MyPkg; use v5.18; use strict; use warnings; use Data::Dumper; use feature qw(state say); use Scalar::Util qw(blessed); use Cwd qw(cwd abs_path); use parent "MyPkgParent"; use PkgSpecificNeed; 1;

...which I would like to have read

package MyPkg; use MyCommonUses; use parent "MyPkgParent"; use PkgSpecificNeed; 1;

Is there some special way to construct a putative MyCommonUses.pm such that its details are not spread throughout my code? A bonus would be some way to dispense with the inelegant one-semicolon.

Thank you in advance,

-SM

Replies are listed 'Best First'.
Re: Header file management
by swl (Parson) on Apr 30, 2018 at 09:31 UTC

    You could adapt the approach used in Modern::Perl (as one example).

    As for not having the 1; at the end of the file, that's needed by the system. It can be any true value, though, so you could replace it with something amusing/interesting if that's your preference.

    Update shortly after posting. See Perl Module ending without 1; for a description of why it is needed, as well as listing and linking to some alternate endings.

      The semicolon isn't required, just the true value :-) In my opinion, removing the semicolon prevents you (or other maintainers of the package) from adding more lines below the final line as it would probably cause a syntax error. (I don't write semicolons after return and die etc. for the same reasons.)

      I usually use

      __PACKAGE__

      at the end of a package. Each module on successful use then returns its own name which should be true unless you plan to name your module 0.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        (I don't write semicolons after return and die etc. for the same reasons.)

        This is only useful if they are positioned at the very end of a scope. Used as argument to statement modifiers without ending semicolon causes a syntax error at compile time, unless you put the semicolon at the beginning of the following line.

        Or is it that I didn't get what you mean? in that case, please show me a working example.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        I've decided to do it your way (__PACKAGE__). I also realized from your post that I was doing the one-semicolon thing wrong anyway. =D

        Thanks again,

        -SM

        Thanks choroba, that's useful to know.

Re: Header file management
by haukex (Archbishop) on Apr 30, 2018 at 12:12 UTC

    There are several modules on CPAN that can do this for you (the AM post provided a few links). I've used Syntax::Collector, here's a working example based on your post:

    package MyCommonUses; use warnings; use 5.018; use Syntax::Collector q{ use strict 0; use warnings 0; use feature 0 ':5.18'; use Data::Dumper 0; use Scalar::Util 0 qw(blessed); use Cwd 0 qw(cwd abs_path); }; 1;

      That was just the thing I needed, thank you!

      The required version thing threw me a bit. As it turned out, I needed to guess where to stick the zero with no if

      no if 0 $] >= 5.018, warnings => "experimental::smartmatch";

      I looked at the source for Syntax::Collector. Rewriting that code not to require a version might be a useful exercise, but not for me today.

      Thanks Again!

      -SM

        no if 0 $] >= 5.018, warnings => "experimental::smartmatch";

        As you know, smart matching is experimental, which means it may change significantly or even go away in future versions of Perl. In the long run, you may want to avoid it until it becomes more clear which direction it's heading in.

Re: Header file management
by Your Mother (Archbishop) on Apr 30, 2018 at 14:26 UTC

    You got great technical answers already. I was pleased to learn from haukex about Syntax::Collector myself. What I want to address is this–

    I have sought my answer in your pages without success, perhaps for want of the proper keywords. Apologies if my humble query has been addressed already.

    This isn't StackOverlflow. PerlMonks welcomes all genuine questions and the monks understand that ideas and practices evolve. Your conscientiousness is appreciated and you can always feel comfortable asking. A question might indeed have been answered, maybe even answered perfectly, in 2003, or 2017, for that matter, but a smarter monk or a better module might be available in 2018, and the aggressive closing of questions as duplicates is part of why I, at least, dislike SO.

      You could tell from my use of header files, I was thinking back to my C++ days. I didn't know how to describe the concept in searchable Perl terms, but felt certain this had to have been addressed using different terms. I am a sojourner from the Kingdom of the Noun, and find myself trying to replace at least some of the best-practice idioms I'd normally use to govern all this spontaneity. ; )

      I've found a lot of useful information on SO, but do find it a bit harsh concerning unoriginal questions.

Re: Header file management
by kcott (Archbishop) on Apr 30, 2018 at 09:44 UTC

    G'day smaines,

    Welcome to the Monastery.

    Firstly, you have some redundant lines which wouldn't need to go into MyCommonUses.pm. The line:

    use v5.18;

    automatically gives you these:

    use strict; use feature qw(state say);

    Although, consider writing it like this (see use for an explanation):

    use 5.018;

    In terms of your main question, there's something very similar to that on CPAN: Modern::Perl. The source code for that module may provide pointers for creating your MyCommonUses.pm.

    — Ken

Re: Header file management
by shmem (Chancellor) on Apr 30, 2018 at 09:49 UTC

    There are many ways to to that.

    Study Modern::Perl by chromatic for a nice way to import modules as a bundle into your current namespace.

    If you use 5.18, then the use feature qw(state say) is redundant afaik.

    You could also write a file, say, header.pl containing use statements,and write your MyCommonUses.pm as

    package MyCommonUses; use strict; use warnings; my $header = '/path/to/header.pl'; my $content = do {local $/; open my $fh,'<',$header or die; <$fh> } or die "couldn't load common use statements, aborted"; sub import { my $caller = caller; eval "package $caller; $content;"; strict->import; warnings->import; } 1;

    so you have to edit only the common header file to add or remove packages.

    A bonus would be some way to dispense with the inelegant one-semicolon.

    Easy. Skip 1;, write

    "sigh."

    at the end.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      The link to Modern::Perl doesn't resolve as you expect.

      The node resolves that to http://perldoc.perl.org/Modern/Perl.html, while swl's link resolves to https://metacpan.org/pod/Modern%3A%3APerl.

      I can't see the original node shortcuts, so I've investigated a bit here:

      mod://Modern::Perl: http://search.cpan.org/perldoc?Modern%3A%3APerl

      metamod://Modern::Perl: https://metacpan.org/pod/Modern%3A%3APerl

      You probably tried this:

      doc://Modern::Perl: http://perldoc.perl.org/Modern/Perl.html

      According to Link Shortcuts, that "links directly to a specific document by name: [doc://name]". In this case, '::' is translated to '/', or [doc://Some::Doc::Name] should point to http://perdoc.perl.org/Some/Doc/Name.html. Let's see: Some::Doc::Name: http://perdoc.perl.org/Some/Doc/Name.html. Yes!

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        The link to Modern::Perl doesn't resolve as you expect.

        Corrected, thank you. s/doc/mod/

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      I've gone with Syntax::Collector, but studying Modern::Perl was very informative, thanks!

      LOL, "sigh",

      -SM

Re: Header file management
by Anonymous Monk on Apr 30, 2018 at 09:38 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1213800]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found