Questions tagged [bless]

Bless is a function in Perl, which associates an object with a class.

In , in order to use references (hard references, not soft references) as objects of a class (or package), we need to associate references with a class first. This is done by using the bless function.

21 questions
13
votes
1 answer

Damn it, Perl can't bless my object

Puns aside, I'm trying to implement an import method in my Perl class to instantiate a Data object, which is essentially a glorified array of hashrefs, from a proper array of hashrefs. Here's an example of how I plan to use it: # Pull in the data my…
Zaid
  • 36,680
  • 16
  • 86
  • 155
10
votes
5 answers

Perl: How to deep copy a blessed object?

I'm looking to do a deep (at this point, shallow may suffice) copy of a blessed object. Foo Class package Foo; our $FOO = new Foo; # initial run sub new { my $class = shift; my $self = {}; bless $self, $class; return…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
7
votes
2 answers

Perl: elegant way to check if something is object blessed as package?

I want to check if a $thing is an object blessed as a package (e.g. __PACKAGE__). One idea is: use Scalar::Util qw(blessed); defined blessed $thing && blessed $thing eq __PACKAGE__ Is there a better and/or more elegant way that avoids checking if…
Moh
  • 304
  • 2
  • 8
6
votes
4 answers

To bless or not to bless, that is my question!

first post from a newbie-user. Every question I google seems to bring me here and I always get a great answer to what I'm looking for; so naturally this was my first stop when I began pondering the usage of blessing in Perl. I've just gotten into…
NiRC
  • 61
  • 2
4
votes
2 answers

In Perl, what's the underlying difference between a hash and a blessed reference?

I am new to Perl, and I would like to understand/know more about the OO parts. Say I have a "class" with only attributes; are there benefits/advantages for creating a package and blessing a hash over working on a hash directly? for simplicity, lets…
yshuki
  • 55
  • 1
  • 5
3
votes
1 answer

How to Bless Objects in a List Passed to a Subroutine?

In my Perl (v5.30.0) script, I have the world's simplest object: #!/usr/bin/perl use warnings; use strict; use Data::Dumper; package Thingee; # Constructor with name new sub new { my $type = shift; my %params = @_; my $self…
Pete
  • 1,511
  • 2
  • 26
  • 49
3
votes
1 answer

Cryptic Moo (Perl) Error "Attempt to bless into a reference at..."

Probably a long shot but I'm wondering if anyone has seen an error like this before, as I can not reproduce it outside of a production environment. Essentially the situation is as follows: I have a module called My::Budget::Module (renamed for…
mshirlaw
  • 168
  • 1
  • 10
2
votes
1 answer

How can I bless a string in Perl?

I am trying to bless a string variable -- demonstrated in the code below. Bless only seems to work when I use a hash or array. Are you allowed to bless strings? If no, what can you bless? I have been debugging for a while, any help would be…
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
2
votes
2 answers

What is difference between bless and tie in perl?

I was practicing some interview questions where I found this. I had a look at perldoc -f tie but couldn't get it. I know about bless and is using in my programs. Can anyone tell me what is tie and how it is related and different from bless, and its…
Kamal Nayan
  • 1,890
  • 21
  • 34
2
votes
3 answers

Perl read_config sub, oop or not?

I have a package (really just one subroutine) I use frequently to parse config file etc. Basically it looks like this: sub get_settings { my %config; my $config = 'path...'; unless(-r $config) { die("Couldn't…
Lasse A Karlsen
  • 801
  • 5
  • 14
  • 23
2
votes
1 answer

Blessing a JSON into a perl class vs private properties

I have a Json structure such as: { "field1" => "one", "field2" => "two", ... } I'm using the perl Json module, and I can bless the Json returned to me into a class, such as: my $result = bless($json->{output},'MyClass') So far so good - now I…
user3653270
  • 166
  • 1
  • 8
2
votes
2 answers

perl using bless self in script and PerlCritic

I am used to using $self for OO Perl even when I am just writing self-contained scripts without naming my package at all. For example, my script.pl would start like this: use strict; use warnings; my $self = bless…
719016
  • 9,922
  • 20
  • 85
  • 158
2
votes
3 answers

How to use Bless Operator in Perl?

can anyone please explain the importance and how to use bless in perl. I have read many threads on stack overflow on bless but they are not clear.
Rahul Reddy
  • 12,613
  • 10
  • 22
  • 21
1
vote
3 answers

Why does Math::Cartesian::Product return blessed objects?

I noticed Math::Cartesian::Product returns an array of blessed objects instead of a simple array of arrays. I couldn't figure out why. I actually need to do some extra work (unbless) to use the results...
David B
  • 29,258
  • 50
  • 133
  • 186
1
vote
1 answer

Attempt to access upserted_id property in perl MongoDB Driver returns useless HASH(0x3572074)

I have a Perl script that pulls a table from a SQL database ($row variable) and attempts to do a MongoDB update like so: my $res = $users->update({"meeting_id" => $row[0]}, {'$set' => { "meeting_id" => $row[0], …
awimley
  • 692
  • 1
  • 9
  • 29
1
2