Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
One of the issues that Object-Oriented programming has blessed us with is data encapsulation. For the most part it's a boon. Unfortunately, If you look at some of the standard code out there that gets used in Objects in Perl, people have the ability to assign things to objects that have no business being there.

When i wrote this originally it was to make a fine distinction between calling an Object's accessor method and calling the item explicitly. What i did was set up the object so that all the items (in a hash) had a leading underscore; no big deal. My accessor methods have no leading underscore, thus the distinction.

But rather than force people to use a leading underscore, i wanted to make the underscore optional. So i came up with the following (both versions).

Later i realized this is a good practice (IMHO) just so that people don't add things to your object hil-nil. i've been using it ever since...

jynx


ps Please tell me of any errors or bugs or improvements you can think of for the code.

pps Sorry if there's bad formatting, there's no preview button in the Snippets Section!!!

# Both have the following pragmas in place: #!/usr/bin/perl -w use strict; use Carp; #================================================= #Version 1: no extra variable (slightly confusing) #================================================= # Get the keys of @_ foreach (keys %{ +{ @_ } }) { # Check to see if a key has a leading underscore ($self->{"_$_"} = ${ +{@_} }{$_}),next if (exists $self->{"_$_"} +); # Check to see if a key has no leading underscore (exists $self->{$_}) ? $self->{$_} = ${ +{@_} }{$_} : # Warn the user if the key isn't valid carp "The entry $_ is not a valid key.\n"; } #================================ # Version 2: a spare %tmp to help #================================ # Get the keys of @_ my %tmp = ( @_ ); foreach (keys %tmp) { # Check to see if a key has a leading underscore ($self->{"_$_"} = $tmp{$_}),next if exists $self->{"_$_"}; # Check to see if a key has no leading underscore (exists $self->{$_}) ? $self->{$_} = $tmp{$_} : # Warn the user if the key isn't valid carp "The entry $_ is not a valid key.\n"; }

In reply to Stripping bad entries from a constructor by jynx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found