Problem Passing data between subroutines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kelicula
    Recognized Expert New Member
    • Jul 2007
    • 176

    Problem Passing data between subroutines

    Hey everyone!
    I am back.
    I have a script that handles the private messaging for a site I am working on and I created a subroutine to handle the sending of a message.

    I want to check if the "to" field was left blank, contains a user name that doesn't exist or has invalid characters in it.

    Everything worked fine, until I integrated the sending of messages to multiple recipients. What I did was create another subroutine to "prepare" the sending. ie: loop through the different user names, and extract proceeding, or trailing whitespace, and them send it to the "send message" subroutine.

    Now for some reason it cannot tell if the user left the "to" field blank.
    No matter what perl considers the "$recepient " to be true. I've tried:

    [code=perl]

    if(!$recepient) {
    do something..
    }

    # AND

    if($recepient !~ /[a-zA-Z]/g){
    really bad but was just testing..
    }

    # AND
    if(length $recepient <1){
    do something
    }
    [/code]

    Nothing works...
    If I open a message and leave the "to" field blank, and click send, I just get the "Message Sent!" message...


    I can't understand it. I get the appropriate error message for both of the other "if" checks...

    Here is the code.
    [code=perl]
    my $error = qq~<font color="red"><sm all><ul>~;

    sub sendMessage {

    my ($recepient, $title, $body) = @_;

    $sth = $dbh->prepare(qq~SEL ECT id, online, email, newsletter FROM users WHERE username=?~);
    $sth->execute($recep ient)|| Tools->listErr( $sth->errstr );
    my @rece = $sth->fetchrow_array ;
    $sth->finish;

    if(!$rece[0] && $recepient){
    $error .= qq~<li>That user: $recepient, does not exist. Make sure you punctuated correctly</li>~;
    }

    if($recepient eq '' || $recepient eq undef){
    $error .= qq~<li>You left the "To" field blank!</li>~;
    }

    if($recepient =~ /[^a-zA-Z0-9_\-\s]/){
    $error .= qq~<li>You have invalid characters in the "To" field.<br /> You may only use a-z, A-Z, 0-9, spaces,<br /> underscores and dashes.</li>~;
    }

    unless(length $error > 29){

    $sth = $dbh->prepare(qq~ INSERT INTO pms (user, recepient, title, body) values(?, ?, ?, ?)~);
    $sth->execute($use r->{id}, $rece[0], $title, $body)|| Tools->listErr( $sth->errstr );

    if(($rece[1] == 0) && ($rece[3] == 1)){
    my $message = qq~You have a new "Private Message" from $user->{username}, waiting for you on \nLiveAudioMag! \n\nClick The link below to log-in and read your message\nhttp://www.liveaudioma g.com/cgi-bin/index.cgi\n\nDo not reply to this message, it was auto-generated and the replies are not read. \nTo unsubscribe to these notifications, Uncheck the "Keep me up to date on LiveAudioMag news, and features." checkbox in your profile edit page.~;
    Tools->sendmail('"Liv eAudioMag" <webmaster@live audiomag.com>', "$rece[2]", 'New Private Message!', "$message") ||
    Tools->listErr( $! );
    }
    }
    return $error;
    }



    sub prepSend {

    my @recepient = split(/,/, $q->param('to')) ;
    my $title = $q->param('subject ')|| "none";
    my $body = $q->escapeHTML( $q->param('body' ) );

    for(@recepient) {

    $_ =~ s/^\s|\s$//g;

    if(length &sendMessage($_ , $title, $body) > 29){
    $error .= qq~<br />Click "Back" on your browser to retrieve message, and resolve error.</ul></small></font>~;

    Tools->create2( 'compose', 'pm', {
    user => $user,
    cgi => $q,
    error => $error,
    contacts => $contacts
    }) || Tools->listErr( $Tools::t->error );
    exit(0);
    }
    }

    $error = qq~<font color="blue">Me ssage Sent!</font>~;
    &openInbox($err or);
    }[/code]

    It's just a snippet, but I'm SURE the problem lies within this code.

    When someone sends a message the "prepSend" sub is called.

    Before I broke up the work, and had one sub to handle it all (only taking one user name) it worked fine.
    The error WILL work if the user name is not in the database, or contains invalid characters, but it will not seem to notice that the "to" field is blank.

    Is this something to do with arrays?
    I was under the impression that null, undef, 0, '' are all the same thing to perl; false.

    Anyone see my mistake? (Other than the misspelling of recipient..hehe he)
    Thanks in advance...
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    At the end of sub prepSend you have:

    Code:
    $error = qq~<font color="blue">Message Sent!</font>~;
    &openInbox($error);
    it looks like that will always be evaluated unless the exit() function is called before that point in the code. And since $error is defined as above, that is always the message it will contain and send to sub openInbox.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I suggest you add some print commands to watch what the script is doing while it runs if you haven't done that already.

      Comment

      • Kelicula
        Recognized Expert New Member
        • Jul 2007
        • 176

        #4
        Yes, I have the sendMessage routine return the error, so that I can see it's length.
        The error message begins with 29 characters if an error occurs it gets perpended to, thus lengthening the string. And I do "exit(0)" after that. See lines 50 through 60.

        The error checking works for invalid characters or a user name that doesn't exist.
        It just seems that by taking a part of an array, and passing it, there is always a "true" value. Even if it's blank. Maybe it is recognizing the reference to a memory allocation?

        But I am not using a reference?

        Can't figure it. I have tried printing the value of $_ and it is blank (to the naked eye)

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Something else is going on because this code seems to work, which is an attempt to simulate your code with some dummy data:

          Code:
          use strict;
          use warnings;
          
          my $to = 'fee,fi, foe ,  , fum';
          my @recipient = split(/,/,$to);
          
          for (@recipient) {
             $_ =~ s/^\s|\s$//g;
             print "[$_] ", checkIt($_), "\n";
          }
          
          sub checkIt {
          my $error;
          my ($recepient) = @_;
          my @rece = ();# to simulate a blank $rece[0]
          
          if(!$rece[0] && $recepient){ # this one should always be true since there is no $rece[0]
             $error .= qq~<li>That user: $recepient, does not exist. Make sure you punctuated correctly</li>~;
          }
           
          if($recepient eq '' || $recepient eq undef){
             $error .= qq~<li>You left the "To" field blank!</li>~;
          }
          
          if($recepient =~ /[^a-zA-Z0-9_\-\s]/){
             $error .= qq~<li>You have invalid characters in the "To" field.~;
          	
          }
          
          return $error;
          }
          output:

          Code:
          [fee] <li>That user: fee, does not exist. Make sure you punctuated correctly</li>
          [fi] <li>That user: fi, does not exist. Make sure you punctuated correctly</li>
          [foe] <li>That user: foe, does not exist. Make sure you punctuated correctly</li>
          [] <li>You left the "To" field blank!</li>
          [fum] <li>That user: fum, does not exist. Make sure you punctuated correctly</li>

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Try changing this:

            Code:
            $_ =~ s/^\s|\s$//g;
            to:

            Code:
            $_ =~ s/^\s*|\s*$//g;
            although I don't think that is the source of your problem.

            Comment

            • eWish
              Recognized Expert Contributor
              • Jul 2007
              • 973

              #7
              To me this logic does not look correct. I am interpreting this as if $rece[0] is undef and $recipient is defined, print error.

              Code:
              if(!$rece[0] && $recipient){
              
              $error .= qq~<li>That user: $recipient, does not exist. Make sure you punctuated correctly</li>~;
              
              }
              Are you wanting to compare them? If so, the this still will fail because $rece[0] looks like it is holding a numeric value and not an alphanumeric value like $recipient appears to contain.

              I hope that I am not completely off here....

              --Kevin

              Comment

              • Kelicula
                Recognized Expert New Member
                • Jul 2007
                • 176

                #8
                Originally posted by eWish
                To me this logic does not look correct. I am interpreting this as if $rece[0] is undef and $recipient is defined, print error.

                Code:
                if(!$rece[0] && $recipient){
                
                $error .= qq~<li>That user: $recipient, does not exist. Make sure you punctuated correctly</li>~;
                
                }
                Are you wanting to compare them? If so, the this still will fail because $rece[0] looks like it is holding a numeric value and not an alphanumeric value like $recipient appears to contain.

                I hope that I am not completely off here....

                --Kevin

                No you have it right. @rec represents a data set from a database query. $recepient is the value entered by the user. So I am saying; if there is no result set from the database, but they DID enter something in the input, print error.

                That is not the check that I'm having a problem from, because if I enter a username that does not exist I get the appropriate error. Which means that
                lines 50 through 60 work fine.

                Also if I enter a user name with invalid characters in it I get that error message too.

                The only thing that doesn't work is the:
                [code=perl]

                if($recepient eq '' || $recepient eq undef){
                $error .= qq~<li>You left the "To" field blank!</li>~;
                }
                [/code]

                Test.

                Scratching my head...

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Like I said, something else is going on because in my example there is a blank entry and it gets caught. See line 4 in the output.

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    OK, I think I see the problem. If the "to" field is blank, the array is never populated with any data here:

                    Code:
                    my @recepient = split(/,/, $q->param('to'));
                    So the subsequent loop is never entered into:

                    Code:
                    for(@recepient){
                    None of the code inside the loop is ever evaluated if the array is empty. Thus the sub &sendMessage () is never called. No error is returned from the sub and your code executes the last lines in sub prepSend:


                    Code:
                    $error = qq~<font color="blue">Message Sent!</font>~;
                    &openInbox($error);
                    Make sense?

                    Comment

                    • Kelicula
                      Recognized Expert New Member
                      • Jul 2007
                      • 176

                      #11
                      Originally posted by KevinADC
                      OK, I think I see the problem. If the "to" field is blank, the array is never populated with any data here:

                      Code:
                      my @recepient = split(/,/, $q->param('to'));
                      So the subsequent loop is never entered into:

                      Code:
                      for(@recepient){
                      None of the code inside the loop is ever evaluated if the array is empty. Thus the sub &sendMessage () is never called. No error is returned from the sub and your code executes the last lines in sub prepSend:


                      Code:
                      $error = qq~<font color="blue">Message Sent!</font>~;
                      &openInbox($error);
                      Make sense?
                      KevinADC you rock!
                      That is exactly the problem.
                      I changed it to check for the existence of @recepient and now it works.

                      Thank you!!

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        You're welcome.

                        Comment

                        Working...