Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

GotToBTru's scratchpad

by GotToBTru (Vicar)
on Jun 15, 2010 at 13:25 UTC ( #844863=scratchpad: print w/ replies, xml ) Need Help??

OO versus Procedures

One example of the difference is what is called data encapsulation. In an OO program, there (should be) no data outside of a context. In a procedure, I might have a variable called $user_name. Any code I have that can see that variable might also modify it. That can be okay, but it also can be a problem. It can make it hard to determine what part of my code changed something. Also, just because I called it $user_name, I could store ANY string in that variable, just for convenience sake. Heck, in Perl, I could reuse it to store a hash reference if I wanted. Why would I? No idea. But I could.

In an OO program, the user will have a name, but it will be an attribute, "name" of a class, "user". The ONLY way to modify and display the name will be to call the code in the user object to do it. If I want sometimes to display the full name, and sometimes the full name and title, and sometimes just the first name, I write the code to format that in the way I like once, and that functionality is now available to any other program that uses my user object. If I have users in my RPG, or in my music collection program, or in my website business, or whatever. Every time, I get every clever function that I have invented for displaying or using user names without having to copy code from one procedure to another, and without having to remember .. did I do that differently here than there? Will making them the same break something? I finally figure out how to handle names like Beyonce, Dr. Jose Juan Carlos Espinosa del Reyza III, and Seven of Nine. Now, every program knows how to deal with them and will do so consistently.

Okay, couldn't you do this with packages, or by being consistent in your programming? Much of it, you could. But it would not be enforced. At anytime, you could say "I'll do this just once because I'm in a hurry." Using OO, I am using the features of the language to enforce good programming practice. That makes it much easier to share what I've done with others.

XML to Hash

sub mapxml { my $xml = shift; my ($tag, $value, %xmlhash); my @lines = (split /\n/, $xml); chomp @lines; while (@lines) { my $line = shift @lines; ($tag,$value) = ($line =~ /<([\w:]+?\/?)>([^<]*)/); next if (! defined($tag) || (substr($tag,-1) eq '/')); if ($value =~ /[\w:-\\]+/) { $xmlhash{$tag} = $value; } else { my $contents = ""; while (1) { $line = shift (@lines); last if ($line eq '</' . $tag . '>'); $contents .= $line . "\n"; } $xmlhash{$tag} = mapxml($contents); } } return \%xmlhash; }

On the Subject of Off Topic

If Perlmonks decides to get more formal in its definition of off-topic, I'd suggest the following:

  1. Add "Off Topic" as a moderation selection, as an alternative to "Approve" or "Frontpage"
  2. Add "Off Topic" as consideration category, an alternative to keep/edit/reap/nada
  3. Allow users a selection to suppress nodes designated OT. This could be as simple as posting possible CSS language


use Test::Simple tests => 2; ok(1 == 1, 'Identity theorem'); ok( &ask eq 'yep', 'User response test'); sub ask { print "Are you a human?\n"; $answer = <STDIN>; chomp $answer; return($answer); }

Link to use as starting point for CSS/JS to color user names to reflect their age relative to me (based on the cb_author id):

CSS to display level

Code code for coding

use strict; use warnings; my $plaintext = 'We are discovered. Flee at once!'; my $key = 'CorrectBatteryHorseStaple'; print "Original message: $plaintext\n"; my $ciphertext = cypher($plaintext,$key); my $represent = $ciphertext; $represent =~ tr/[A-Za-z0-9] /*/c; print " Encoded message: $represent (* indicates non-alphabet characte +r)\n "; my $decoded = cypher($ciphertext,$key); print " Decoded message: $decoded\n"; sub cypher { my $plain = shift; my $key = shift; my $i=0; $key x= ( 1 + length($plain)/length($key) ); return join '', map {$_ ^ substr($key,$i++,1)} (split '',$plain); }

Stuff to Check Out


DatePointsPostsRankETA Next RankNext Rank
201407211794154HermitN/AChaplain (1800)

Benchmarking Your Code
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-11-25 19:50 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (688 votes), past polls