Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

by Discipulus (Canon)
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 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 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/ # 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


    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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2023-09-29 14:39 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found