tobyink / p5-zydeco

Perl 5 distribution Zydeco; see homepage for downloads and documentation.
https://zydeco.toby.ink/
14 stars 3 forks source link

Allowing the use of the include keyword with .pm files #4

Closed smonff closed 3 years ago

smonff commented 3 years ago

Hi,

I am testing Zydeco and enjoy it.

I tried to use the include keyword and I am wondering why it disallow to actually "include" .pm files? Browsing the source I only see that .pl is mentionned, making impossible to include from anything else than a .pl file.

Any reason for this? Maybe I am missing the point by trying to separate roles and classes in different file when Zydeco should maybe be used like a kind of Mojolicious::Lite of OO. Dunno...

tobyink commented 3 years ago

My thinking is that included files only really make any sense in the context of the module that included them. It's unlikely that you'll ever write a file to be included via include that will be included from multiple different callers. They're not like most Perl modules which are designed to be independently usable. You're splitting things up to avoid everything being in one big file rather than for reuse.

So not using ".pm" is to stop people from expecting to be able to use them.

I did consider using an entirely different name like ".zydeco", but using ".pl" has the advantage that editors will automatically pick them up as Perl. (And I've added class, role, etc to my editor's list of keywords.)

smonff commented 3 years ago

Thanks for the clarification.

It is a different paradigm that Zydeco offers and I was thinking too much in a traditional use way. Is there any other tool that use a similar include mechanism for exposing roles and classes that have inspired you?

tobyink commented 3 years ago

I guess include works somewhat more similarly to Perl's do $filename.

smonff commented 3 years ago

Ok, so I continued some experiment and I feel like re-opening this issue.

Strictly speaking, the use of the include keyword in a basic program and/or in tests works fine. But I have issues while using modules building / authoring tools. I tried Minilla, mbtiny, and even more low level perl Build.PL process, and I experienced the same problem of non-found .pl files during the build step.

I tried quite a lot of things about this and read some serious documentation and it was difficult to find use cases of .pl files in lib/: I always thought it was not a thing, but why not :D...

I dunno how you solved those kind of problems in real life applications, but I found some stories about .pm.PL files. Looks like those things would be understood by build / authoring tools. It is especially written in Module::Build documentation that a file lib/Foo/Bar.pm.PL could create the file lib/Foo/Bar.pm. The .PL files are processed first, so any .pm files (or other kinds that we deal with) will get copied correctly. Otherwise, as far as I can understand, the .pl files in lib/ are not copied anywhere.

I hope it could useful. If I miss a point, I am always happy about learning.

Thanks for reading.

tobyink commented 3 years ago

I don't really use Module::Build or Module::Build::Tiny much myself, apart from for a few distributions I took over from other authors who use them.

ExtUtils::MakeMaker definitely supports .pl files in lib though.

Test release: https://metacpan.org/release/TOBYINK/Acme-ZydecoTesting-App1-0.001

Initial results from CPAN Testers are all green: http://fast-matrix.cpantesters.org/?dist=Acme-ZydecoTesting-App1%200.001

Of course, CPAN Testers often don't install the distribution; just download, build, and run tests. So you can test it gets installed correctly using:

cpanm Acme::ZydecoTesting::App1
perl -MAcme::ZydecoTesting::App1 -E'my $app="Acme::ZydecoTesting::App1"; say $app->new_foo'

For what it's worth, there are ways to structure an app into multiple files without include anyway. For example:

MyApp.pm:

use v5.14;
package MyApp {
  BEGIN { our $VERSION = '0.01' };
  use Zydeco;
  use MyApp::Stuff;
  use MyApp::MoreStuff;
}
1;

MyApp/Stuff.pm:

use v5.14;
package MyApp {
  use Zydeco;
  role Bar;
}
1;

MyApp/MoreStuff.pm:

use v5.14;
package MyApp {
  use Zydeco;
  class Foo;
  class FooBar extends Foo with Bar;
}
1;

Note that the MyApp/Stuff.pm and MyApp/MoreStuff.pm files don't define packages called "MyApp::Stuff" and "MyApp::MoreStuff" but continue the definition of "MyApp". (They need to re-use Zydeco because it has a lexical effect.)

I haven't tested this much (this is the closest thing I have to a proper test for it) but it ought to work.

Zydeco is still pretty experimental and I'm still figuring out the best way of doing some things.

smonff commented 3 years ago

Thanks for all this, it really helps. I guess the problems are on my side and/or because of limitations of some high level build tools I use.

I saw the Acme app on CPAN this morning and was actually searching for something like this before.

I'll take a deeper look tomorrow since I am not so familiar of ExtUtils::MakeMaker, it's always good to learn.

About the structuring of this example app you described above, I tried something similar first (without the includes) but not with the strict package MyApp { } namespace 😅.

I also wondered about the experimental status of Zydeco: maybe it could be nice to have an explicit mention of it in the doc, because the fancy website and everything could suggest it is not so experimental.

I hope those comments helps but feel free to tell me if it's irrelevant.

tobyink commented 3 years ago

I've checked out Module::Build::Tiny and it only supports .pm and .pod in lib.

https://metacpan.org/release/Module-Build-Tiny/source/lib/Module/Build/Tiny.pm#L88

Module::Build's code is… well…. there's a lot of it. And I'm not entirely sure what it supports.

I think the solution might be to switch to a double extension .zydeco.pm. That should satisfy syntax highlighters looking for Perl-like file extensions, it should keep installers happy, and it should stop people from trying to loading it with use. (And if the file doesn't exist, I'll load .pl as a falllback.)

smonff commented 3 years ago

I'll test this right away on Minilla!

tobyink commented 3 years ago

It's not released yet.

tobyink commented 3 years ago

Released now, and I've also done another release of Acme::ZydecoTesting::App1

smonff commented 3 years ago

Minilla is eating it very well. My initial tests with the include keyword pass :1st_place_medal:

Thanks!