Re: Can I learn more about blessed data?
by LanX (Saint) on Jan 08, 2021 at 00:28 UTC
|
You are seeing objects.
"Blessed" means a reference was augmented to an object by associating a class i.e. a package. Any method ->call will be resolved in by subs in that package (or its inheritance chain)
See bless and perlobj for details.
Your question is too general to be better answered, provide examples if you want more help.
| [reply] [d/l] |
|
|
Your question is too general to be better answered, provide examples if you want more help.
My apologies. What I meant was depending on what I'm doing or who's module I am using I get data with just one !VAR, or sometime many more.
I feel like I do when I first started using Regex. Never thought I'd get there. Then I found a great book, and then the holy grail...
RegEx101. Using that site I was able to figure my way around many different type of grep routines.
So I was hoping to find something like that, that would help tutor me.
| [reply] |
|
|
I see perlootut hasn't been suggested yet. Start there. Then go to bless and perlobj.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] |
|
|
> Then I found a great book, and then the holy grail...
Look, imagine someone asking for a book about "Churches", because he needs to understand this "Religion" (which is probably Christianity)...
That's how confusing your question sounds.
Blessing is just a mechanism from Object Oriented Programming.
you could also see it from a "typing" perspective:
Perl has data with primitive types, like arrays, hashes and scalars (including subtypes like integer, string, etc.)
These types have properties and methods. like length or keys
Blessing is a way to construct new types, with new properties and methods.
OOP is one application of that, and 99% of all objects you'll encounter in Perl are just blessed hashrefs.
There is no definitive book which will list all blessed data, because every module can create it's own, you have to check the documentation.
> I'm doing or who's module I am using I get data with just one !VAR,
Sounds like you are using Data::Dumper to display some JSON data.
JSON derives from Javascript which has an explicit "type" (sic) Boolean which doesn't exist in Perl. It's represented by "1" and ""/0 there, there is no TRUE and FALSE.
BUT when reading JSON data you often need to distinguish between the "1" and TRUE.
That's why JSON converters create a Perl object for boolean to represent this type
And Dumper will show something like bless \1, "Boolean" for true (or something similar)
HTH! :)
| [reply] [d/l] |
|
|
|
|
Re: Where can I learn more about blessed data? -- a basic example
by Discipulus (Canon) on Jan 08, 2021 at 08:22 UTC
|
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.
| [reply] [d/l] [select] |
Re: Can I learn more about blessed data?
by Bod (Parson) on Jan 08, 2021 at 00:12 UTC
|
I can't tell if blessed or unblessed
To determine if an object has been blessed or not, use the Scalar::Util blessed function.
use strict;
use Scalar::Util 'blessed';
if ( defined blessed($some_scalar) ) {
# ...do something...
}
| [reply] [d/l] [select] |
|
|
perl -E "say ref {a=>33}"
HASH
perl -E "say ref bless{a=>33},'Some::Package'"
Some::Package
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.
| [reply] [d/l] |
|
|
To determine if any reference is blessed or not, I would highly recommend Ref::Util's is_blessed_ref() subroutine. That is precisely what it is designed for.
I strongly suspect that determining the blessedness of a given reference is not what SergioQ was actually enquiring about, however.
Update: sample test set to show it in action. You can throw anything at it and it won't complain - just return true if the arg is a blessed ref and false otherwise.
use strict;
use warnings;
use Ref::Util 'is_blessed_ref';
use Test::More tests => 9;
ok ! is_blessed_ref (undef), 'No: undef';
ok ! is_blessed_ref (0), 'No: false';
ok ! is_blessed_ref (1), 'No: true';
my ($x, @x, %x);
ok ! is_blessed_ref (\$x), 'No: scalar ref';
ok ! is_blessed_ref (\@x), 'No: array ref';
ok ! is_blessed_ref (\%x), 'No: hash ref';
bless \$x, 'Foo';
bless \@x, 'Foo';
bless \%x, 'Foo';
ok is_blessed_ref (\$x), 'Yes: blessed scalar ref';
ok is_blessed_ref (\@x), 'Yes: blessed array ref';
ok is_blessed_ref (\%x), 'Yes: blessed hash ref';
| [reply] [d/l] [select] |
Re: Can I learn more about blessed data?
by 1nickt (Canon) on Jan 08, 2021 at 02:02 UTC
|
How are you getting this "blessed" data? Sounds like you're dumping an object, or seeing a stracktrace. Please describe your situation and provide data samples.
The way forward always starts with a minimal test.
| [reply] |