develooper Front page | perl.perl6.language | Postings from March 2005

some misc Perl 6 questions

Thread Next
Darren Duncan
March 8, 2005 15:50
some misc Perl 6 questions
Message ID:

I have just started to write a large amount of Perl 6 code, partly to 
help with testing the Pugs (and later Parrot) implementation of the 
language.  During this process, I discovered a few details about Perl 
6 that I don't yet understand, and haven't yet been able to get 
answers to in my reading of the Synopsis.  So I'll ask about them 

For some of these points, I just wanted clarification on something I 
think I already know, and for others its just a "how do I" matter:

0. As I understand it, the current state of being says that all Perl 
6 code will by default "use strict" and "use warnings" (y/n), unless 
it's a one-liner?

1. What is the behaviour of require() in regards to what it does when 
a module or class was previously defined in a file that was already 
loaded, but that file didn't have a file name that corresponded to 
the module or class?  Eg, say a program determines at runtime that it 
wants to use module Foo, and that module is defined in a file named 
Bar that declares both the Bar and the Foo module.  If the program 
had already loaded file Bar sometime earlier, then will "require Foo" 
use the existing declaration, or try to load a file named "Foo" but 
fail?  Or will something else happen.  Essentially, I want users of 
my module to be able to declare a data-containing module using either 
method, its own file or shared in another file, and my module to just 
see and use it.

For reference, this is what I currently have to do in Perl 5:

   no strict 'refs';
   my $package_is_loaded = defined %{$template_module_name~'::'};
   use strict 'refs';
   unless( $package_is_loaded ) {
     require $template_module_name;
   $text = $template_module_name->get_text_by_key( $message.msg_key );

I do that because Perl 5 require() always attempts to load a matching 
file name.

2. Related to the above, if the require() of Perl 6 will indeed try 
to load a file with the matching name, then what is the proper way to 
test if a module was already loaded?  Hopefully this is elegant, 
rather than the Perl 5 "no strict 'refs'" cludge.  ('cludge' means 
anything I need to un-strict to accomplish.)

3. Does my explicit new() method get a "$self:" argument that is 
already instantiated like other methods, or do I have to do something 
like a bless() on my own declared variable of the class type and 
explicitly return that?

4. Is it possible to create a 'class' that sub-classes a 'module' or 
can one only subclass a 'class'.  The reason I'm declaring the parent 
as a module is because it doesn't have any attributes, but it does 
have utility functions that I wish for users of multiple sub-classes 
to be able to invoke off of them.

5. If I have a class property defined using "has Str;", and an 
object of the class is named "$bar", then is it correct syntax to 
refer to the property as a whole using "$" and an element 
using "${'abc'}"?

6. I understand that sub/method arguments are passed in read-only by 
default.  However, if (see #2) I do a "return( $ );" when will 
the caller getting that return value have received a reference to or 
a copy of the attribute?

7. Say I have 2 arrays of arrays, @foo and @bar.  What is the most 
concise Perl 6 syntax for each of these operations in Perl 5?  The 
Perl 5 equivalent is expressed where each is an array having array 
refs that are unalike in size.

   @foo = @bar  # a shallow copy
   @foo = map { [@{$_}] } @bar  # a deep copy

8. Is it possible with a sub or method argument signiture to specify 
that the arguments must have defined values?  Or are specced 
non-optional arguments only guaranteed to have the correct container 
passed to them?

9. What does the as() method on built-in types do on a hash that has 
undefined values?  Does it raise a "use of undefined value in string" 
warning, or just substitute the empty string?

10. When using as(), can I specify that the results get sorted?  Or 
alternately, will hashes always serialize in the same order on all 
platforms, unlike in Perl 5 where its pairs come out in an apparently 
random order?  If not, I will have to continue to do manually what 
as() does.

Okay, that's it for my initial set of questions.

Thanks in advance for any feedback, whether they are plain answers or 
pointers to where *exactly* in the Perl 6 spec I should successfully 
find the answer.

-- Darren Duncan

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About