Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

reference versus bless

by arcnon (Monk)
on Jan 15, 2005 at 15:45 UTC ( [id://422511]=perlquestion: print w/replies, xml ) Need Help??

arcnon has asked for the wisdom of the Perl Monks concerning the following question:

I have been looking a bit for the advantages? differences in bless and references to complex data structures. After doing some reading from my "programming perl","perl objects, references & modules" and "advanced perl programming".

I dont feel I am any closer to some answers than when I started.

Do blessed objects exist merely to provide a intereface to the data or am I lost? You have to write accessors and constructors for both.

Can you enlighten as to the whys? Or point me somewhere so I can find out?

Replies are listed 'Best First'.
•Re: reference versus bless
by merlyn (Sage) on Jan 15, 2005 at 15:56 UTC
    References create complex data structures (hashes of hashes, etc). Objects associate code with a particular collection of similar data structures so that common operations can be accessed, and reused for similar structures. Blessing is the mechanism that associates a package name with a structure, so that the code can be found for that structure.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: reference versus bless
by brian_d_foy (Abbot) on Jan 15, 2005 at 20:28 UTC

    bless() attaches a package name to a reference. If you treat that reference as an object (i.e. call methods on it), that package name tells perl in which package to start looking for that method.

    You can still treat a blessed reference as a regular ol' reference, but you can't treat a regular ol' reference as an object. A blessed reference is just a reference with an associated package name.

    The blessed reference doesn't provide the interface: it just tells perl which package has it. You have to provide the interface yourself, either directly in that package or in something it inherits.

    --
    brian d foy <bdfoy@cpan.org>
Re: reference versus bless
by Tanktalus (Canon) on Jan 15, 2005 at 15:52 UTC

    I'm not entirely clear on the question - you pretty much cannot have blessings without references (at least not in perl5).

    The purpose of bless'ed objects (references) is the ability to declare which package (or package tree if inheritance is used) to look for methods.

    You can have references to built-in types (scalar, array, hash, glob), or to types created by bless (hopefully, the name of a package that has been loaded).

    So ... I'm just not sure what you're referring to - perhaps some example code of what you're talking about might be in order?

Re: reference versus bless
by steves (Curate) on Jan 15, 2005 at 16:26 UTC

    To clarify just a bit, you can have references that aren't blessed. As merlyn points out, references to hashes and arrays are used to build complex (often nested) data structures. Perl also offers anonymous hashes and arrays, which are references.

    blessing a reference essentially makes it an object. In addition to holding and providing access to its data in the normal ways, a blessed reference also can be used to invoke methods that in the package it's been blessed into. If that package also makes use of @ISA inheritence, then you also inherit methods in a pretty standard object oriented sort of model.

Re: reference versus bless
by arcnon (Monk) on Jan 16, 2005 at 17:00 UTC
    CLAIRIFICATION TO MY FIRST POST

    I guess maybe a good answer must be constituted from code.

    I am just using a reference to my data structure. Is there an advantage to changing it to a blessed reference? most of my accessors are generic but a few are specialized. I seek your thoughts and or possible change ideas.

    As you can see I put the data into a array then reference it to a hash so I can get at it easily then return the whole thing as a reference. The reason for the array is so I can loop through it to write out the converted data in case your wondering. Also I am following a (MVC) model, view, controller programming style. This is part of the data model.

    main.pl


    my $buffer = Data::File::Open('all.bin'); my %Header = Data::Struct::Define::Header::Get($buffer); my $objREF_wp = Data::Struct::Wp::wpData(substr($buffer, $Header{ws}{s +tart}, $Header{ws}{offset})); $value = Data::Struct::Wp::n_1_1($objREF_wp);

    Wp.pm


    sub wpData{ my $buffer = shift; my $binString = unpack('b*', substr($buffer, 0)); my @data; my %objREF; my $count = 0; open(F, "Waypoints.txt") or die $!; my @funct = <F>; close(F); foreach my $line (@funct) { $line=~s/\r|\n//g; my @element = split(/,/, $line); my $value = substr($binString, $element[1], $element[2]); #### NO BYTES SWAPPING FOR THIS STRUCT $data[$count] = $value; $objREF{$element[0]} = \$data[$count]; $objREF{array} = \@data; $count++; } $objREF{static} = "yes"; $objREF{instream} = "$binString"; $objREF{size} = "81"; $objREF{end} = substr($binString, (((81 * 8) - (length(join('', @d +ata)))) - 1)); return \%objREF; } sub n_1_1{ ##### IN: [obj-ref,new value or nothing to return value] my $objREF = shift; if (@_){ my $value = shift; my $bitstream = ${$objREF->{n_way}}; substr($bitstream,0,1) = $value; ${$objREF->{n_way}} = $bitstream; } else{ my $bitstream = ${$objREF->{n_way}}; my $value = substr($bitstream, 0, 1); return $value; } }

      I'm gone try to give a (partially) response to your question by formulating it otherwise. I understand your question as: "Which is better: use objects (blessed reference) or use structs (just references) to pass my data around in my application?"

      The answer on this rephrased question is not easy because the answer is partly depending on your situation and partly about the principle.

      If you are using blessed references, you're chosing the object oriented approach. If you are using simply references, you take the procedural approach. Both sides have their supporters, advantages and disadvantages.

      Should you use objects? Depends on a couple of things. Are you familiar with OO? Are you familiar with OO in perl? Is this a simple one shot script or does will it be a part of a bigger system? Is your input data well structured and stable in time or is it subject of changes? Is the data heavily interconnected with other data? Do you need fast access? Will your application grow or is it a small script?... I refer you to the literature available about the subject for more information.

      By looking at you'r code I personally would use the OO approach because it would confine all business logic about waypoints in one place. That should make you're code a lot easier to maintain.

      But hey, I'm someone who uses OO all the time, even if it sometimes overkill...

      The moral off the story: I think you can't really expect a clear answer because the answer is: it depends...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2024-04-23 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found