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

Add simple use example #4497

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions doc/Language/101-basics.rakudoc
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,34 @@ say "we have @flavors.sort.join(', ')";
# OUTPUT: «we have peach, vanilla␤»
=end code

=head1 Calling a raku method from another file

To call a raku subroutine from another file, the convention is to have a minimal
raku module or class file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To address @lizmat's concern, maybe we can note this is intended to be a simplistic example and point to how to write a module for a more... realistic? larger? example?


=begin code
# MyClass.rakumod
class C {
has $.x;
}
=end code

Then your script can use this file.

=begin code
# myscript.raku
use MyClass;

my $instance = C.new(x=>42);
say $instance.x;
=end code

The C<-I.> directive tells the raku interpreter to look in directory C<.>.

=begin code
Copy link
Collaborator

@coke coke Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lang<shell> so we don't try to compile it

> raku -I. myscript.raku
=end code

=head1 Exercises

B<1.> The input format of the example program is redundant: the first line
Expand Down