A few days ago I was trying to figure out how to get a function module return multiple values as a list. Setting the first value to 1 or 0 depending on if it had been sucessful or not and the rest of the list as the results.

One of the replies was by Ryszard who reponded "make sure you take an OO approach..". And I thought why my list method is great and my module is awesome. Then I though about it. Instead of having this list and values to be verified I can only return those in a certain order. What if I don't care about the other items. What if I don't need them just at that moment. What if I want to return other stuff.

So I rewrote my module and now I have five or six methods that return everything upon request ( get methods )and allow me to add things like the HTML error string for when the error occurs so it is uniform through scripts. And later on I can add things like a description of what the error means so the end user can see what it is.

So when thinking about a multiple return situation try an object.
# NOTE NOT PASTING FULL CODE JUST RELEVANT PARTS #module Gatekeeper allows for a standard password and user #access method for PUCK access. package Gatekeeper; use DBI; use Digest::MD5 qw(md5_hex); #time to lock out users who have failed login 3 times #in seconds my $lockout_time = 180; #number of attempts before system locks users out my $max_failed_login_attempts = 3; #html error strings that are used for creating blocked login #pages for the end user to see my $login_blocked = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Login Blocked please wait 3 minutes and try +again<\/font>"; my $username_malformed = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Re-enter username<\/font>"; my $password_malformed = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Re-enter password<\/font>"; my $username_invalid = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Username not found<\/font>"; my $password_invalid = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Password not found<\/font>"; my $password_blank = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Please enter password<\/font>"; my $username_blank = "<font face=\"Arial, Helvetica, sans-serif\" +size=\"2\" color=\"red\"> Please enter username<\/font>"; #constructor sub new { my $self = {}; $self->{user_id} = undef; $self->{login_valid} = undef; $self->{error_type} = undef; $self->{html_error_string } = undef; bless( $self ); return( $self ); } sub get_html_error_string { my $self = shift; return( $self->{html_error_string} ); } sub get_error_type { my $self = shift; return( $self->{error_type} ); } sub get_password_valid { my $self = shift; return( $self->{login_valid} ); } sub get_user_id { my $self = shift; return( $self->{user_id} ); }

edited: Mon Nov 18 15:37:46 2002 by jeffa - added <reamore> tag


In reply to Returing Multiple Items and possibly using an object by Angel

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.