Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Mark Leighton Fisher (4252)

Mark Leighton Fisher
  (email not shown publicly)

I am a Systems Engineer at Regenstrief Institute []. I also own Fisher's Creek Consulting [].
Friday June 30, 2006
11:30 AM

Design by Contract: Perl6/VB Preconditions and Postcondition

[ #30121 ]

Like I've said, Perl5 lvalue subs by themselves are not useful for Design by Contract-style data validation preconditions and postconditions, as you cannot examine the data on input or output (but check out the comments on that post). No such limitation exists with Perl6 rw subs and Visual Basic Properties, so let's see some examples of data validation preconditions and postconditions.

This Perl6 example should work in the current Pugs (tested on the Win32 Pugs of June 6, 2006, 04:35 EST). A variable that can only have the values of:

  • The empty string,""; or
  • A specific URL

can be implemented in Perl6 today with an rw sub as:

my $url_value = "";
sub specific_url is rw {
     if ($url_value ne "") {
          $url_value = "";
     } else {
          $url_value = "";
     return $url_value;

The sequence:

specific_url() = "";
say "specific_url = ", specific_url(), "\n";
specific_url() = "";
say "specific_url = ", specific_url(), "\n";

yields this output:

specific_url =
specific_url =

As specified above, specific_url() can only be assigned a value of "". Any other attempt at assignment causes specific_url() to take on a value of "".

Although VB Properties were originally designed for exposing the adjustable parameters of Windows controls (comboboxes, timers, etc.), they also provide a dandy way to give your VB variables their preconditions and postconditions. A VB class property for U.S. Social Security numbers would look like this (along with a 'notSS' property for comparison purposes):

Public Property Let SocialSecurity(newSocialSecurity As String)
    If Not newSocialSecurity Like "###-##-####" Then
        mSocialSecurity = ""
        mSocialSecurity = newSocialSecurity
    End If
End Property
Public Property Get SocialSecurity() As String
    SocialSecurity = mSocialSecurity
End Property

Public Property Let notSS(newNotSS As String)
    mNotSS = newNotSS
End Property
Public Property Get notSS() As String
    notSS = mNotSS
End Property

SocialSecurity can only be assigned a validly-formatted U.S. Social Security number, yet code that uses the SocialSecurity Property looks just like code that uses a VB String for a Social Security number:

    ss1.notSS = "300-42-980A"
    ss1.SocialSecurity = "300-42-980A"
    ss2.SocialSecurity = "300-42-9805"

    Debug.Print "notSS = '" & ss1.notSS & "'"
    Debug.Print "ss1 = '" & ss1.SocialSecurity & "'"
    Debug.Print "ss2 = '" & ss2.SocialSecurity & "'"

yields this output:

    notSS = '300-42-980A'
    ss1 = ''
    ss2 = '300-42-9805'

As you can see, the SocialSecurity property only accepts a validly-formatted U.S. Social Security number, but the plain String-valued Property accepts any old String value.

There's much more to Design by Contract than preconditions and postconditions, of course, but this brief essay should give you a leg up on this important technique for easing the pain of data validation.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.