in reply to reference versus bless

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

Replies are listed 'Best First'.
Re^2: reference versus bless
by martell (Hermit) on Jan 16, 2005 at 19:42 UTC

    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...