I have the following code:
class MyStupidString {
method repeatit(Str $string, Int $repeat) {
say $string xx $repeat;
my $add = $string xx $repeat;
say $add;
}
};
my $obj = MyStupidString.new;
$obj.repeatit("---",10);
Interestingly, it says:
> pugs test2.p6
------------------------------
--- --- --- --- --- --- --- --- --- ---
What am I misunderstanding here? Or is this a Pugs bug?
I am using pugs 6.2.12
- Fagzal
The `xx` operator. That's list-repeat, not string-repeat.
In Perl 5 terms, the code you wrote is:
sub repeatit {
my ( $string, $repeat ) = @_;
my @res = ( $string ) x $repeat;
print @res;
my $add = "@res";
print $add;
}
which should make it obvious what is going on.
--
GMX DSL-Flatrate 0,- Euro* - Überall, wo DSL verfügbar ist!
NEU: Jetzt bis zu 16.000 kBit/s! http://www.gmx.net/de/go/dsl
List context.
> my $add = $string xx $repeat;
Item context, for a list repetition operator. Doesn't really make sense,
and I think a warning or error message would be more appropriate.
I think you meant either:
my @add = $string xx $repeat;
or:
my $add = $string x $repeat;
Or perhaps:
my $add = [ $string xx $repeat ];
# This is what your current code does, but I think it's best if Perl
# enforced that you be explicit about the [].
--
korajn salutojn,
juerd waalboer: perl hacker <ju...@juerd.nl> <http://juerd.nl/sig>
convolution: ict solutions and consultancy <sa...@convolution.nl>
>>I have the following code:
>>
>>class MyStupidString {
>> method repeatit(Str $string, Int $repeat) {
>> say $string xx $repeat;
>> my $add = $string xx $repeat;
>> say $add;
>> }
>>
>>};
>>
>>my $obj = MyStupidString.new;
>>$obj.repeatit("---",10);
>>
>>
>>
>>Interestingly, it says:
>> > pugs test2.p6
>>------------------------------
>>--- --- --- --- --- --- --- --- --- ---
>>
>>
>>What am I misunderstanding here?
>>
>>
>
>The `xx` operator. That's list-repeat, not string-repeat.
>
>In Perl 5 terms, the code you wrote is:
>
> sub repeatit {
> my ( $string, $repeat ) = @_;
> my @res = ( $string ) x $repeat;
> print @res;
> my $add = "@res";
> print $add;
> }
>
>which should make it obvious what is going on.
>
>
Thank you and Juerd.
It was indeed a misunderstanding, I though "x" changed to "xx" in Perl6
for some reasons...
Now is the first time I write some Perl6 code that I actually run, too
:) It is *really* enjoyable. Feels like "this is how Perl should really
look like", and it is so DWIM I can hardly believe it. Kudos to all who
helped to make it happen!
- Fagzal