Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Storing Info with Accessors

by bikeNomad (Priest)
on Aug 01, 2001 at 20:21 UTC ( [id://101432]=note: print w/replies, xml ) Need Help??


in reply to Storing Info with Accessors

If your object is maintaining a reference to the supplierList, you can just splice it if you want to. The reference remains an intact reference to the same array. But actually, in your code, I don't see why you need to rebuild anything, since you've only modified a hash that is referred to by an array that is referred to by self. You haven't changed the object structure in any way.

As an aside, you've chosen a rather roundabout way to make accessors with that string eval. Why not just use a closure:

sub _create_accessors { my %orderObjectFields = @_; foreach my $field ( keys %orderObjectFields ) { my $default = $orderObjectFields{$field}; no strict 'refs'; *{ __PACKAGE__ . "::$field" } = sub { my $value = ( scalar @_ > 1 ? $_[0]->{$field} = $_[1] : $_[0]->{$fie +ld} ); $value = $default unless defined $value; $value; }; } }

Replies are listed 'Best First'.
Re: Re: Storing Info with Accessors
by graq (Curate) on Aug 02, 2001 at 12:30 UTC
    As an aside, you've chosen a rather roundabout way to make accessors with that string eval. Why not just use a closure:

    I am, in a few months, going to be looking at various parts of the project and sprucing up the code. But I am wary of no strict 'refs'. I really do prefer to avoid using such methods. Your suggestion has given me ideas though, thanks :)

    --
    Graq

      Since no strict 'refs' only affects the block it's in, it won't affect the rest of your program (past the next curly brace, anyway). I make a practice of putting it in the innermost scope possible. You can do the following instead if you're paranoid. Note that the no strict 'refs' only affects the one line with the assignment. Why would you hesitate to use that? I hope you're not absorbing the 'always use strict everywhere' dogma...

      foreach my $field ( keys %orderObjectFields ) { my $default = $orderObjectFields{$field}; my $closure = sub { my $value = ( scalar @_ > 1 ? $_[0]->{$field} = $_[1] : $_[0]->{$fie +ld} ); $value = $default unless defined $value; $value; }; no strict 'refs'; # only affects next statement: *{ __PACKAGE__ . "::$field" } = $closure; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found