Re: How to get indirect access to a class attribute?

2015-03-25 Thread Bruce Gray

On Mar 25, 2015, at 7:44 AM, Tom Browder tom.brow...@gmail.com wrote:
—snip--
 1. How can I indirectly refer to the attributes in a method?  The
 above doesn't work (with or without the '()’).

—snip—

use v6;
our %attrs = (
   age = 1,
   # wgt = 2, # Need to handle No such method before uncommenting.
);
class foo {
   # has $.age = rw;
   has $.age;

   method all_attrs {
   my @ordered_attrs = %attrs.sort({ + .value }).map({ .key });
   for @ordered_attrs - $k {
   my $aval = self.$k();  # supposed to work for a method name
   say attr { $k } has value '{ $aval }';
   }
   }

}

my $f = foo.new( age = 30 );
# say $f.perl;
$f.all_attrs();

My modified version of your code (above) works for me. Output is:
   attr age has value '30'

Note that your line has $.age = rw was invalid syntax,
and your a method was *outside* your original foo class.

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)



Re: How to get indirect access to a class attribute?

2015-03-25 Thread Tom Browder
On Wed, Mar 25, 2015 at 8:47 AM, Tom Browder tom.brow...@gmail.com wrote:
 On Wed, Mar 25, 2015 at 8:29 AM, Moritz Lenz mor...@faui2k3.org wrote:
 the indirect method call syntax is the right approach, you just got too
 many other details wrong to make it work.

This syntax works in a method as you said:

  self.$elem()

Again I was getting errors that masked the correctness of that
syntax--incomplete debugging!

Thanks all.

Cheers!

-Tom



 Fair enough--my fingers fumbled a few important things.  I'll correct
 and recheck;

 Thanks, Moritz (and Bruce).

 Cheers!

 -Tom


How to get indirect access to a class attribute?

2015-03-25 Thread Tom Browder
Given a class like:

our %attrs = (age=1,wgt=2);
class foo { has $.age = rw;}

method a {
  for %attrs.kv - $k, $v {
 my $aval = self.$k();  # supposed to work for a method name
 say attr { $k } has value '{ $aval }';
  }
}

Question:

1. How can I indirectly refer to the attributes in a method?  The
above doesn't work (with or without the '()').

2. Do I have to write a custom accessor to do so?

Thanks.

Best regards,

-Tom


Re: Re-installation of Perl 6 (Rakudo Star) via rakudobrew on Linux

2015-03-25 Thread Steve Mynott
Although it usually works I've seen some oddities (maybe as minor as
one test spec test failure)  recently with rakudobrew where it doesn't
cleanly rebuild (i assume there are traces of a previous build
confusing it).

The easiest thing is to delete everything and start again.
S

On 24 March 2015 at 14:53, Tom Browder tom.brow...@gmail.com wrote:
 I installed the 2015.02 version of Perl 6  (Rakudo Star) by following
 these instructions on the perl6.org site:

 quote

 To install Rakudo and Panda using rakudobrew:

 rakudobrew build moar
 rakudobrew build-panda

 Finally, install Task::Star. This will install all the modules that
 are shipped with the Rakudo Star Perl 6 distribution:

 panda install Task::Star
 /quote

 Since I saw no words regarding installation of a new version I blindly
 repeated the instructions above, and I see my perl6 is indeed updated
 to 2015.03.

 However, I got this error message during the last step (panda install
 task::Star) for the last module (LWP::Simple):

 quote

 == Fetching LWP::Simple
 == Building LWP::Simple
 Compiling lib/LWP/Simple.pm to mbc
 == Testing LWP::Simple
 t/000-load-module.t . ok
 t/basic-auth.t .. ok
 t/custom-headers-and-content.t .. ok
 t/get-binary-camelia.t .. ok
 t/get-chunked-6guts.t ... ok
 t/get-perl6-org.t ... ok
 t/get-unsized.t . ok
 t/get-w3-latin1-utf8.t .. ok
 t/get-w3-redirect.t . ok
 t/getstore.t  ok
 t/parse-url.t ... ok
 Failed to connect: connection timed out
   in method initialize at src/gen/m-CORE.setting:24980
   in method new at src/gen/m-CORE.setting:24964
   in block unit at t/socket-sanity.t:6
 t/socket-sanity.t ...
 Dubious, test returned 255 (wstat 65280, 0xff00)
 Failed 2/2 subtests
 t/stringify-headers.t ... ok
 Test Summary Report
 ---
 t/socket-sanity.t (Wstat: 65280 Tests: 0 Failed: 0)
   Non-zero exit status: 255
   Parse errors: Bad plan.  You planned 2 tests but ran 0.
 Files=13, Tests=53, 79 wallclock secs ( 0.05 usr  0.02 sys + 10.71
 cusr  0.95 csys = 11.73 CPU)
 Result: FAIL
 test stage failed for LWP::Simple: Tests failed
   in method install at lib/Panda.pm:125
   in block  at lib/Panda.pm:1
   in method resolve at lib/Panda.pm:185
   in sub MAIN at
 /home/tbrowde/.rakudobrew/bin/../moar-nom/install/languages/perl6/site/bin/panda:20
   in sub MAIN at
 /home/tbrowde/.rakudobrew/bin/../moar-nom/install/languages/perl6/site/bin/panda:18
   in block unit at
 /home/tbrowde/.rakudobrew/bin/../moar-nom/install/languages/perl6/site/bin/panda:77


 Failure Summary
 
 Task::Star
 *test stage failed for LWP::Simple: Tests failed

 /quote

 Questions:

 1.  Is there a better or proper way to upgrade to a new Rakudo Star release?

 2.  Is the failure of LWP::Simple a bug or just some weird
 misconfiguration on my system?

 Thanks.

 Cheers!

 -Tom



-- 
4096R/EA75174B Steve Mynott steve.myn...@gmail.com


Re: How to get indirect access to a class attribute?

2015-03-25 Thread Moritz Lenz
Hi,

On 25.03.2015 13:44, Tom Browder wrote:
 Given a class like:
 
 our %attrs = (age=1,wgt=2);
 class foo { has $.age = rw;}

should be 'has $.age is rw'. The is indicates a trait (not an
assignment).

 method a {
   for %attrs.kv - $k, $v {
  my $aval = self.$k();  # supposed to work for a method name


Etiher method a needs to be inside class foo, or it needs to be a
subroutine, and refer to foo instead of self here.

A method outside a class doesn't ususally make sense, which is why you
get this message:

Other potential difficulties:
Useless declaration of a has-scoped method in mainline (did you mean
'my method a'?)

  say attr { $k } has value '{ $aval }';
   }
 }
 
 Question:
 
 1. How can I indirectly refer to the attributes in a method?  The
 above doesn't work (with or without the '()').

the indirect method call syntax is the right approach, you just got too
many other details wrong to make it work.

Cheers,
Moritz