Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword 'module' disables automatic calling of MAIN sub when file is run as a program. #2372

Closed
p6rt opened this issue Feb 17, 2011 · 5 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Feb 17, 2011

Migrated from rt.perl.org#84342 (status was 'rejected')

Searchable as RT84342$

@p6rt
Copy link
Author

p6rt commented Feb 17, 2011

From @molecules

SHORT VERSION (notice lack of output when file is run)​:

$ cat lib/A.pm
use v6;
module A;

our sub MAIN ( $filename ) {
  say "Preparing to process '$filename'";
}

$ lib/A.pm
$ lib/A.pm foo.txt

LONG VERSION​:
I would like to make what brian d foy calls a 'modulino', a module
that can act as a program or as a module. This makes
Test-Driven-Development much easier, as you can test it like a module
but run it like a program.

However, using the 'module' keyword makes it cease working as a program.

Without the 'module' keyword, the following works fine as a program,
but not as a module. (I really like the automatically generated usage
message, by the way).

$ cat lib/A.pm
use v6;

our sub MAIN ( $filename ) {
  say "Preparing to process '$filename'";
}

$ lib/A.pm foo.txt
Preparing to process 'foo.txt'

$ lib/A.pm
Usage​:
lib/A.pm filename

shell returned 29

Then with the keyword 'module', it doesn't work right. Notice the lack
of a usage message and the lack of output.

$ cat lib/A.pm
use v6;
module A;

our sub MAIN ( $filename ) {
  say "Preparing to process '$filename'";
}

$ lib/A.pm
$ lib/A.pm foo.txt

Thanks,
Christopher Bottoms (molecules)

@p6rt
Copy link
Author

p6rt commented Apr 1, 2015

From @usev6

This was answered recently on SO​: http://stackoverflow.com/questions/29281305/how-do-i-make-a-modulino-in-perl6

The recommended solution was "to add MAIN to the module and import it into the script's mainline. This way, everything declared within the module is visible within MAIN without having to explicitly export everything​:

module main {
  sub process(@​filenames) {
  for @​filenames -> $filename {
  say "Processing '$filename'";
  }
  }

  sub MAIN(*@​filenames) is export(​:MAIN) {
  process(@​filenames);
  }
}

import main :MAIN;

@p6rt
Copy link
Author

p6rt commented Aug 27, 2015

From @coke

On Wed Apr 01 11​:46​:59 2015, bartolin@​gmx.de wrote​:

This was answered recently on SO​:
http://stackoverflow.com/questions/29281305/how-do-i-make-a-modulino-
in-perl6

The recommended solution was "to add MAIN to the module and import it
into the script's mainline. This way, everything declared within the
module is visible within MAIN without having to explicitly export
everything​:

module main {
sub process(@​filenames) {
for @​filenames -> $filename {
say "Processing '$filename'";
}
}

sub MAIN(*@​filenames) is export(​:MAIN) {
process(@​filenames);
}
}

import main :MAIN;

Based on bartolin's last comment, closing this ticket.
--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Aug 27, 2015

The RT System itself - Status changed from 'new' to 'open'

@p6rt p6rt closed this as completed Aug 27, 2015
@p6rt
Copy link
Author

p6rt commented Aug 27, 2015

@coke - Status changed from 'open' to 'rejected'

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant