Skip to content

Commit 09016b7

Browse files
committed
Document %?RESOURCES
1 parent a24093f commit 09016b7

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

doc/Language/variables.pod6

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,58 @@ C<$?FILE> and C<$?LINE> are also available from L<CallFrame|/type/CallFrame> as
11921192
the L<C<file>|/type/CallFrame#method_file> and
11931193
L<C<line>|/type/CallFrame#method_line> methods, respectively.
11941194
1195-
=head3 Other compile-time variables:
1195+
=head3 X<%?RESOURCES>
1196+
1197+
A compile-time variable available within code of a X<Distribution>.
1198+
1199+
It contains a hash that provides compile and runtime access to files
1200+
associated with the Distribution of the current compilation unit. It
1201+
is used to access a special storage for Distribution-wide static files
1202+
(for example, examples of configuration files, templates etc).
1203+
1204+
To add a file to a distribution, it is placed under C<resources> directory:
1205+
1206+
=begin code :lang<text>
1207+
Module-Foo/
1208+
├── lib
1209+
│ └── Module
1210+
│ └── Foo.pm6
1211+
├── META6.json
1212+
├── README.md
1213+
└── resources
1214+
└── images
1215+
└── foo.jpg
1216+
=end code
1217+
1218+
Then a relative path (starting from root directory of a distribution)
1219+
to a file is specified under C<"resources"> field in META6.json file:
1220+
1221+
=begin code :lang<js>
1222+
"resources": [
1223+
"images/foo.jpg"
1224+
]
1225+
=end code
1226+
1227+
Every resource file is added to an installed Distribution and is
1228+
accessible using a Hash-like access to C<%?RESOURCES>:
1229+
1230+
=begin :skip<resources-example>
1231+
my $foo-IO = %?RESOURCE<images/foo.jpg>; # gets an object you can slurp
1232+
my $foo-IO = %?RESOURCE<images/foo.jpg>.absolute; # gets an absolute path to a file
1233+
my $foo-IO = %?RESOURCE<images/foo.jpg>.open; # gets an opened IO::Handle to work with
1234+
=end code
1235+
1236+
Note that paths and names of resource files can be mangled in an
1237+
installed distribution, so do not rely on their values in any other
1238+
case besides using them as keys for C<%?RESOURCES> variable.
1239+
1240+
C<%?RESOURCES> variable is not implemented as a plain Hash, but an
1241+
instance of C<Distribution::Resources> type, so do not expect to see
1242+
all available resource files in a distribution by printing or other
1243+
ways to inspect its value. Instead, use the API described above to
1244+
access particular files.
1245+
1246+
=head3 Introspection compile-time variables
11961247
11971248
X<|$?PACKAGE>X<|$?MODULE>X<|$?CLASS>X<|$?ROLE>
11981249
X<|$?TABSTOP>X<|$?NL>X<|$?DISTRIBUTION>
@@ -1211,6 +1262,8 @@ The following compile time variables allow for a deeper introspection:
12111262
With particular regard to the C<$?NL>, see the L<newline
12121263
pragma|/language/pragmas>.
12131264

1265+
=head3 Rakudo-specific compile-time variables
1266+
12141267
These variables are Rakudo specific, with all the corresponding caveats:
12151268
=for table
12161269
$?BITS Number of data-path bits of the platform the program is being compiled upon.

0 commit comments

Comments
 (0)