Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Where can I learn more about blessed data? -- a basic example

by Discipulus (Abbot)
on Jan 08, 2021 at 08:22 UTC ( #11126597=note: print w/replies, xml ) Need Help??


in reply to Where can I learn more about blessed data?

Hello SergioQ,

Your question is not very clear to me. What do you mean with: "messy blessed text messes"? I searched for something in your recent posts, quickly, I cant find something relevant.

You got good replies (see LanX's one). As he said bless is used to add something like a flag to a reference. The flag says: I'm not from here. I come from Whatever::Package so see there to see how I behave. When some method of this blessed reference is called (a method is simple sub) the first parameter received will be the object itself, generally named $self but just as convention.

A full minimalist example

use strict; use warnings; # usually this is defined inside a perl module # at ./Whatever/Package.pm package Whatever::Package; # this is the constructor. generally called new but is not a rule sub new{ # we will receive the class name as first argument my $class = shift; # and just need to return a reference (an anonymous hash {} in thi +s case) blessed into $class return bless {}, $class; } sub first { my $self = shift; print "We are inside package ",__PACKAGE__, " we received \$self because it is blessed into ",ref $sel +f,"\n"; } # usually this is defined inside a perl module # at ./Another/Example/Package.pm package Another::Example::Package; # note the same NAME for the sub sub first { my $self = shift; print "This method will no be called unless the object is blessed +into Another::Example::Package"; } # usually the script will use Whatever::Package (defined inside a perl + module ./Whatever/Package.pm) # but we can merely switch back to the default package 'main' package main; # again a sub with the same name sub first{ print "We are inside package ",__PACKAGE__," I'm not a method but +a bare lone sub\n"; } # test the local sub first(); # let 'instanciate' an object of the Whatever::Package class my $obj = Whatever::Package->new(); print "\$obj is now a blessed reference. It is blessed into ",ref $obj +,"\n"; # above we have 3 subs named first. Which one will be called? $obj->first();

Start with the standard documentation. See package too.

If you can get a copy of the old but valid Perl CookBook read:

  • Chapter 11: References and Records
  • Chapter 12: Packages, Libraries, and Modules
  • Chapter 13: Classes, Objects, and Ties

    L*

    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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: note [id://11126597]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2022-01-18 14:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      In 2022, my preferred method to securely store passwords is:












      Results (53 votes). Check out past polls.

      Notices?