pack function
Hi there, I am trying to translate some old perl 5 code and I am having trouble with a particular line. I used to have this in perl 5 to translate multibyte encoding like "%C3%A1": $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/eg; And I have translated the regex to something like this: $value ~~ s:g/[\%(<[\dA..Fa..f]><[\dA..Fa..f]>)]+/{pack().decode()}/; So I thought I could just change the base of the matching hex bytes and pack them, but the pack function is not working as it did in perl 5. I tried many things and the closest solution I came to implied using the "template" argument of the pack function like this: print pack("CC", 195, 161).decode(); This produces the character "á". As you can see, I am adding as many "C"s as bytes there be. And I can count the elements in the regex match to construct the template, but I'm wondering if there is a better approach. All of your comments on the matter would be much appreciated. Kind Regards, Emiliano
Re: Need help with Nil values
Thanks, That was it. Somtimes I get confused with the way other languages treat undefined/null/nil values. Regards, Emiliano From: Timo PaulssenSent: Tuesday, February 23, 2016 2:20 AM To: perl6-users@perl.org Subject: Re: Need help with Nil values Hello Emiliano, In this case, I think you may want to use just "Str" instead of "Nil". "Str" is the "type object" for Str objects, and you can check whether it's a string like "foo" or just the Str object by checking $!value.defined. There's a FAQ answer that's about "Any", but it works the same way for "Str": http://doc.perl6.org/language/faq#What_is_%28Any%29%3F And there's the classtut that talks about this a bit more in depth, search for "type object": http://doc.perl6.org/language/classtut I hope this'll get you towards your goal. If not, just keep asking :) Have fun with Perl 6! - Timo
RE: Constants as members of a class
Thanks Liz, I will stick with the method aproach. Regards, Emiliano > Subject: Re: Constants as members of a class > From: l...@dijkmat.nl > Date: Fri, 18 Dec 2015 23:49:53 +0100 > To: perl6-users@perl.org > > > On 18 Dec 2015, at 23:37, TS xx <maringa...@hotmail.com> wrote: > > > > Hi Liz, thanks for your reply. > > > > Can I call the method from static context? > > I mean: MyClass.FOO > > In general, yes: > > class A { > method FOO { 42 } # note the method doesn’t reference any attributes > } > say A.FOO; > say A.new.FOO; # also works on instances > > You can even have a method to be called in case of a static (class) context, > and one to be called in the case of an instance: > > class B { > has $.FOO = 666; > multi method FOO(B:U:) { 42 } # B is the class, :U is undefined, > final : for self > multi method FOO(B:D:) { $!FOO } # :D is defined, > } > say B.FOO; # 42 > say B.new.FOO; # 666 > say B.new(FOO => 3.14).FOO;# 3.14 > > > Hope this helps! > > > > Liz
RE: Constants as members of a class
Hi Liz, thanks for your reply. Can I call the method from static context? I mean: MyClass.FOO > Subject: Re: Constants as members of a class > From: l...@dijkmat.nl > Date: Fri, 18 Dec 2015 10:23:11 +0100 > To: perl6-users@perl.org > > > > On 18 Dec 2015, at 03:46, TS xx <maringa...@hotmail.com> wrote: > > > > Hello dear perl6 users, > > > > I was in the need of declaring a member variable as a constant integer. > > After many syntax tryouts I came to this one: > > > > class MyClass { > > has int $.myConst; > > > > method new () { > > return self.bless(); > > } > > > > submethod BUILD () { > > constant $!myConst = 1; > > } > > > > method showMyConst () { > > print $!myConst; > > } > > } > > > > But I'm getting the followinf error message: "Twigil-Variable constants not > > yet implemented. Sorry." > > > > The only place in the docs where I have found any reference to constants is > > in here: https://doc.perl6.org/language/variables#The_%3F_Twigil > > But it's not what I am looking for :/ > > > > So my questions are: > > Is the syntax right and the thing isn't implemented yet? > > Is the syntax (or the full concept) wrong? > > Do I have and old interpreter (This is perl6 version 2015.02 built on > > MoarVM version 2015.02)? > > Yes, that is ancient in rakudo years :-) > > The question you should ask you: is the constant different for each > instantiated object or not. > > If not, and you only need it inside of your class, you can just make it a > constant in the mainline of the class: > > class MyClass { > constant FOO = 42; > … > } > > If you need to expose a method returning such a constant: > > class MyClass { > method FOO { 42 } > … > } > > Otherwise, you probably just need to add an attribute. > > > > Liz
RE: Constants as members of a class
Thanks anyway The new method is there just to state the example. Date: Thu, 17 Dec 2015 22:58:32 -0500 Subject: Re: Constants as members of a class From: awwa...@thelackthereof.org To: maringa...@hotmail.com CC: perl6-users@perl.org Two things jump out at me. One is that I think you don't need that "new" method. Second -- yes, this is a very old interpreter. I unfortunately don't know about the twigil variable constant things. --Brock On Thu, Dec 17, 2015 at 9:46 PM, TS xx <maringa...@hotmail.com> wrote: Hello dear perl6 users, I was in the need of declaring a member variable as a constant integer. After many syntax tryouts I came to this one: class MyClass { has int $.myConst; method new () { return self.bless(); } submethod BUILD () { constant $!myConst = 1; } method showMyConst () { print $!myConst; } } But I'm getting the followinf error message: "Twigil-Variable constants not yet implemented. Sorry." The only place in the docs where I have found any reference to constants is in here: https://doc.perl6.org/language/variables#The_%3F_Twigil But it's not what I am looking for :/ So my questions are: Is the syntax right and the thing isn't implemented yet? Is the syntax (or the full concept) wrong? Do I have and old interpreter (This is perl6 version 2015.02 built on MoarVM version 2015.02)? Thanks to all. Kind Regards, Emiliano
Constants as members of a class
Hello dear perl6 users, I was in the need of declaring a member variable as a constant integer. After many syntax tryouts I came to this one: class MyClass { has int $.myConst; method new () { return self.bless(); } submethod BUILD () { constant $!myConst = 1; } method showMyConst () { print $!myConst; } } But I'm getting the followinf error message: "Twigil-Variable constants not yet implemented. Sorry." The only place in the docs where I have found any reference to constants is in here: https://doc.perl6.org/language/variables#The_%3F_Twigil But it's not what I am looking for :/ So my questions are: Is the syntax right and the thing isn't implemented yet? Is the syntax (or the full concept) wrong? Do I have and old interpreter (This is perl6 version 2015.02 built on MoarVM version 2015.02)? Thanks to all. Kind Regards, Emiliano
How to call a super class method?
Hello fellow perl users, I have been trying to understand perl 6 oop implementation, and one thing I still can't figure out is how to call super class methods from lower classes. Let's say we have two classes, Person and Employee, and the method I am trying to access is the object constructor: class Person { method new ($argument) { #do some things } } class Employee is Person { method new () { #call here Person's method new #do more things } } Can I call the Person's constructor (in non static context), pass the required parameter and do more things before returning? Regards, Emiliano