Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

[Perl6] say variable in Class

by Edelbaer (Initiate)
on Jun 02, 2018 at 16:26 UTC ( [id://1215745]=perlquestion: print w/replies, xml ) Need Help??

Edelbaer has asked for the wisdom of the Perl Monks concerning the following question:

Hello believers,

I am new to PERL and currently try to gain some ground. My current problem is to use a class property for a SAY statement inside the class like:

class A { has Str $.x ; say 'Test' say $!x ; }

However, that does not seem to work and I do not really understand why.

Anybody with profound knowledge in this respect ?

KR

Edelbaer

2018-06-03 Athanasius added [Perl6] to title and code tags to body text

Replies are listed 'Best First'.
Re: say variable in Class
by mr_ron (Chaplain) on Jun 02, 2018 at 17:34 UTC

    Your code is almost valid Perl 6. In order to inspect attribute $!x you have to construct an object instance leading to the two suggestions below. See the Perl 6 Object Orientation document for more.

    The first approach is a more usual starting point for development.

    class A { has Str $.x ; method test { say 'Test'; say $!x ; } } A.new(x =>'hello').test;

    The second approach is more advanced but may be closer to the OP intentions.

    class A { has Str $.x ; submethod TWEAK { say 'Test'; say $!x ; } } A.new;
    Ron

      Hello Ron,

      thank you for your reply. I think what you mean is that $.x would be accessible in the class as part of a method but not directly. Therefore your first method will work of course but defeats a bit the idea that the object on creation reports its status. The second approach works exactly as it should. And I guess now I need to learn why. So you were absolutely right about the intention :).

      Now I know what to learn, that is real progress!

      Thx a lot!

        but defeats a bit the idea that the object on creation reports its status
        If you want to do that, then you would probably need to create your own constructor with this added feature in it.

        Or, as shown by mr_ron, you might (re)define the TWEAK submethod, which is called by the new default constructor, so that redefining TWEAK effectively modifies the behavior of new.

Re: say variable in Class
by Discipulus (Canon) on Jun 02, 2018 at 16:49 UTC
    Hello Edelbaer and welcome to the monastery and to the wonderful world of Perl!

    this post was written before perl6 tag was added
    your code does not seem at all perl code to me. Even if I'm not at all an ObjectOriented monk, as far as I know class is not used to declare classes. See package

    The keyword has is used in ObjectOriented (OO) perl by modules like Moose and Moo see Moose::Manual to get started.

    Before other attempts go read perlootut as introduction and other introduction manual entries in the same site: perlintro and all the language section starting with perlsyn

    As side read take your opy of the wonderful ModernPerl book and notice that it stress OO on Moose even if this is not the only way do to OO in Perl.

    In my library I have some links about objects

    As final note Perl is the language name; perl is the program; PERL just does not exits

    L*

    PS use  <c> code tags</c> to rend your code in nodes: try to edit your post to use that tags. See Tutorials and Writeup Formatting Tips

    PPS if this is a Perl6 question modify the titel too: [p6]  say variable in Class

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      PPS if this is a Perl6 question modify the titel too:  [p6]  say variable in Class

      No :) The title is for humans, so perl6, perl 6... no braces/rackets/parenthesis needed/wanted/beneficial

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1215745]
Approved by davies
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-16 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found