EzDevInfo.com

bless

CSS Post-Processor Bless

What exactly does Perl's "bless" do?

I understand one uses the "bless" keyword in Perl inside a class's "new" method:

sub new {
    my $self = bless { };
    return $self;
}

But what exactly is "bless" doing to that hash reference ?


Source: (StackOverflow)

How can I unbless an object in Perl?

From perldoc -f bless:

bless REF,CLASSNAME

This function tells the thingy referenced by REF that it is now
an object in the CLASSNAME package.

Is there any way of obtaining an unblessed structure without unnecessary copying?


Source: (StackOverflow)

Advertisements

Error after using Mac OS bless command: "no bootable device"

I've created an applescript to boot from snow leopard to lion, but the bless command is failing. Here's the command:

do shell script "bless -mount /Volumes/bootdrive/ -legacy -setBoot -nextonly" password "mypassword" with administrator privileges

On reboot, I get the black screen with 'No bootable device' error. I've run the command directly in the terminal as root (rather than as an applescript) and have gotten the same result. And yes, I've triple-checked that the disk path I'm using is correct and is bootable.

Any idea what the issue could be?


Source: (StackOverflow)

Perl: referencing/blessing question

The idea is to implement a class that gets a list of [arrays, Thread::Conveyor queues and other stuff] in a TIEHASH constructor,

use AbstractHash; 
tie(%DATA, 'AbstractHash', \@a1, \@a2, \$tcq);

What is a correct way to pass object references (like mentioned Thread::Conveyor objects) thus array references into constructor, so it can access the objects? Any cases when a passed object should be blessed?


Source: (StackOverflow)

Perl : Get the keys of a blessed hashref

There are 2 packages in the code.

Package 1 :

package Foo;
sub new {
    my ($class, $args) = @_;
    my $hashref = {'a' => 1, 'b' => 2};
    bless ($self, $class);
    return $self;
}

Package 2 :

package Fuz;
use Foo;
.
.
.
.
my $obj = Foo->new($args);

How to get the keys of the blessed hashref in the object ?

Am aware of Acme::Damn and Data::Structure::Util modules in perl to unbless the object. Are there any other ways to achieve this ?


Source: (StackOverflow)

unbless implementation in Perl?

As many Perl developers know, unbless is used to remove the blessing from objects.
I am trying to understand how does it internally work by investigating its implementation.
I tried to check its implementation in its package Data::Structure::Util. Here's the Source:

sub unbless {
    unbless_xs( $_[0] );
}

Where may I find unbless_xs sub implementation/source ?


Source: (StackOverflow)

Perl how to get values from bless object request data

I am using RPC::XML and parsing request xml using below code and request xml.

use RPC::XML::ParserFactory 'XML::Parser';
my $P = RPC::XML::ParserFactory->new();
my $parse_data = $P->parse($xml_data); 
print Dumper($parse_data);

Request xml:

<methodCall>
<methodName>get_topic</methodName>
<params>
<param>
<value>
<string>163397</string>
</value>
</param>
<param>
<value>
<i4>0</i4>
</value>
</param>
<param>
<value>
<i4>19</i4>
</value>
</param>
<param>
<value>
<string>ANN</string>
</value>
</param>
</params>
</methodCall>

I have get object data:

bless( {
'name' => 'get_topic',
'args' => [
            bless( do{\\(my $o = '163397')}, 'RPC::XML::string' ),
            bless( do{\\(my $o = '0')}, 'RPC::XML::i4' ),
            bless( do{\\(my $o = '19')}, 'RPC::XML::i4' ),
            bless( do{\\(my $o = 'ANN')}, 'RPC::XML::string' )
          ]
}, 'RPC::XML::request' );

After I have done print Dumper($parse_data->args); and get below args object data:

bless( {

$VAR1 = [
         bless( do{\\(my $o = '163397')}, 'RPC::XML::string' ),
         bless( do{\\(my $o = '0')}, 'RPC::XML::i4' ),
         bless( do{\\(my $o = '19')}, 'RPC::XML::i4' ),
         bless( do{\\(my $o = 'ANN')}, 'RPC::XML::string' )
      ];

Now how to get values from this args object data? please help!


Source: (StackOverflow)

Perl 'bless' to PHP?

I am trying to figure out what does bless do in perl - after reading their documentation - I am not very clear. Correct me if I am wrong, it allow to create properties in the class or object?

Someone coded this block of code

package Main::Call;

sub new
{
    my ($class, $call) = @_;
    my $self = $call;
    bless($self, $class);
    return $self;
}

So for example:

if (($statement = $db->prepare($sql)) && $statement->execute())
{
    while (my $rt = $statement->fetchrow_hashref())
    {
        my $obj = Main::Call->new($rt);
        push @reserv_call_objs, $obj;
    }

    return \@reserv_call_objs;
}

I am trying to convert this to PHP.

So I am assuming it would be like this?

class Call {

 public function __construct($arr) { 
    foreach($arr as $key => $value)
    {
       $this->$value = '';
    }   
 }

 public function __set($key, $value) {
    $this->$key = $value;
 }

}

Source: (StackOverflow)

How does comma separated operations work in perl?

I am currently studying Perl programming and am running into statements like this:

return bless { }, $type;

I know what return bless { }; would do, and I also know what return $type; would do, but how does separating them by a comma affect the statement, and does it work the same way for all unary operators?


Source: (StackOverflow)

Is unblessing perl objects a dreadful design?

Is unblessing Perl objects means having a dreadful design ?
If yes, can any one explain this to me ?
BTW, Here's the discussion which fired this question, check comments on the question


Source: (StackOverflow)

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 can create methods within MyClass.pm to return the values of field1, field2, etc. So it seems that via bless, I have direct access to set the properties of an object. But the danger is that I can also do things later in the code like: $result->{field1} = "anythingIwant";

...which is not so good. I know I could set the property like _field1 to indicate privacy but that doesn't prevent me from doing $result->{_field1} = "anythingIwant";

So is there a best practice approach in perl to handle this situation? In other words, it's super convenient to be able to bless Json output into a class to deserialize, but it also seems dangerous. I'm looking for the best of both worlds where I can still use bless but also prevent any client code from doing the anythingIwant scenario described above. I've looked into Moose, InsideOut etc but I'm not really sure if either of those would fit the bill or just introduce more complexity.


Source: (StackOverflow)

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 read config");
        }
        open CONFIG, '<', $config or die $!;
        while(<CONFIG>) {
                next if (($_ eq "\n") or /^\;/);
                chomp;
                my($setting, $value) = split(/=/, $_);
                $config{$setting} = $value;
        }
        return %config;
}

Pretty basic, but I was wondering how (and if) this could/should be re-written to OOP? Really just for learning, never quite seen when and why to use bless. =)

Thanks!


Source: (StackOverflow)

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 greatly appreciated. :-) If I making an error in my code please let me know what it is.

This is a perl file. The code is not finished, but it never reaches the "Page End" statement. So I have ceased to lengthen it. $FileInfo is an array define earlier read from a file but due to syntax gets garbled here.

here is the call to build ojbect reference

$page = new GeneratePages(0);

package GeneratePages;  
sub new  
{  
    my $class = shift;  
    my $pageContents = $FileInfo[shift];  
    bless $pageContents, $class;  
    return $pageContents;  
}

Source: (StackOverflow)

WWW::Wunderground::API Can't bless non-reference value

I would like to use module WWWW::Wunderground::API to download data with weather using JSON.

Here is my PERL script:

use WWW::Wunderground::API;

my $wun = new WWW::Wunderground::API(location=>'KIAD', api_key=>'my key');
print 'JSON source:'.$wun->json if $wun->api_type eq 'json';

It gives me an error:

Can't bless non-reference value at /usr/local/share/perl5/Hash/AsObject.pm line 82.

I cannot fix it up. I have been trying to update cpan and other modules but it gives no results.

Could you tell me how can I repair it?

Thank you in advance


with Carp::Always:

Can't bless non-reference value at /usr/local/share/perl5/Hash/AsObject.pm line 82 Hash::AsObject::AUTOLOAD('Hash::AsObject', undef) called at /usr/local/share/perl5/WWW/Wunderground/API.pm line 37 WWW::Wunderground::API::update('WWW::Wunderground::API=HASH(0x1e5b178)', 'KIAD') called at /home/xyz/workspace/WeatherTest/scr.pl line 4 eval {...} called at /home/xyz/workspace/WeatherTest/scr.pl line 4


Source: (StackOverflow)

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 $self;
}

Main Program

use Foo;
my $copy = $Foo::FOO;     # instead of creating a ref, want to deep copy here
$copy->{bar} = 'bar';

bar appears in both $Foo::FOO and $copy. I realize I could create a copy of the object by setting it up as $copy = { %{$Foo::FOO} }, but then it would no longer be blessed; additionally, this would only work for simple data structures (right now not an issue). Is the only way to copy this way and then bless after (eg $copy = bless { %{$Foo::FOO} }, q{Foo};)?

I'm trying to avoid using Moose, Clone, or other non-Core modules/packages, so please keep that in mind when replying. Bolded so it stands out more :)


Source: (StackOverflow)