Perl: handling a specific input

& a regular expression question

Page 1 of 1

5 Replies - 1893 Views - Last Post: 11 March 2007 - 11:49 AM

#1 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 199
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Perl: handling a specific input

Posted 10 March 2007 - 06:37 AM

1) I have an odd problem. I am saving multiple values to a text file in a record manor, and all works fine, except for the one variable i am not writting to the file. I have a $d_no value which is implemented at the time of displaying a summary of the records, since i need to count them anyway (something else i implemented), this seemed the best option. My problem is, in my main if i declare: my $d_no = ReadDNo(%in); with ReadDNo:
sub ReadDNo(\%)
{
	my $in = shift(@_);
	if(!defined(${$in}{'d_no'}))
	{
		return 1;
	}
	my ($dno) = (${$in}{'d_no'} =~ /^(\d+)$/);
	return $dno;
}


*Since i am setting the values i am not worried about excess spaces, though i could add it.
This will run, and the default is to display the summary, which works great if it is displayed as: www...../script.cgi but if i run what should be the equivalent of: www...../script.cgi?mode=summary it doesn't work. Basically all of my mode values no longer work, while if i use a constant value my $d_no = 7; they all display fine, but the $d_no is always 7. The $d_no value displays correctly in the address bar, so i my values are found correctly inside of my edit, input, etc methods, but it is not displayed correctly.
Mode switching code just in case it helps:
		if($mode eq 'input')
	{
		#input mode allows a new entry: display form
		PrintFiles(%substitutions, "DLog.html");
	
	}
	elsif($mode eq 'submit')
	{
		if(my $error = saveData(%incoming))
		{
			$substitutions{'ERROR'} = $error;
			PrintFiles(%substitutions, "DLog.html");
		}
		else
		{
			summary(%substitutions);
		}
	}
	elsif($mode eq 'view')
	{
		#details/edit details of a single entry:
		edit(%substitutions);
	
	}
	elsif($mode eq 'summary')
	{
		summary(%substitutions);
	}
	else
	{
		#exit on any other input mode
		die('Error: Unrecognized Mode');
	}


My question basically boils down to:
Is there a way to use the value from the address bar for $d_no instead of the one my cgi is apparently not getting, or is there a better way to deal with this? Any ideas are welcome.


2) I have a comments area which (should) handle multiple words, i've looked around and it seems like i need to use \g, but there isn't a single good explanation on how to read all the words. Technically i don't need to read each word, just the entire text as a whole from the textarea.
I had been using something like this:
sub ReadComments(\%)
{
	my $in = shift(@_);
	if(!defined(${$in}{'comments'}))
	{
		return '';
	}
	my ($comments) = (${$in}{'comments'} =~ /^\s*(\w+)\s*$/);
	return $comments;
}


but this only works for a single word, any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Perl: handling a specific input

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: handling a specific input

Posted 10 March 2007 - 01:27 PM

how are you defining a value for $mode? How are you parsing the name/value pairs sent in the query string to your script? Why are you using prototypes (sub ReadDNo(\%)) instead of subroutines? (sub ReadDNo {)?
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 199
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Perl: handling a specific input

Posted 10 March 2007 - 05:35 PM

how are you defining a value for $mode?
the mode value is passed in via a hidden input type in a form.

How are you parsing the name/value pairs sent in the query string to your script?
the query is a giant hash so i parse by key value. If this isn't what you meant, sorry i'm still getting the hang of a few areas of perl.

Why are you using prototypes (sub ReadDNo(\%)) instead of subroutines? (sub ReadDNo {)?
because it's the best way i know how at the moment, if you want to suggest a better way i'm all ears... or eyes i guess.
Was This Post Helpful? 0
  • +
  • -

#4 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: handling a specific input

Posted 11 March 2007 - 01:03 AM

$d_no is a digit (7), $mode is a string (summary), I don't see how they are connected with the problem you are having. You're code is confusing, I can't see any reason for this construct:

${$in}{'comments'}

It looks like you should be using:

$in{'comments'}

could have something to do with your use of prototypes and typing the data:

sub ReadComments(\%)

I would drop all that prototype stuff, there is no need for it and it just makes like harder. So this:

sub ReadComments(\%)
{
	my $in = shift(@_);
	if(!defined(${$in}{'comments'}))
	{
		return '';
	}
	my ($comments) = (${$in}{'comments'} =~ /^\s*(\w+)\s*$/);
	return $comments;
}



would be written as:

sub ReadComments
{
	my %in = @_;
	if(!defined($in{'comments'}))
	{
		return '';
	}
	my ($comments) = ($in{'comments'} =~ /^\s*(\w+)\s*$/);
	return $comments;
}


Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 199
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Perl: handling a specific input

Posted 11 March 2007 - 07:48 AM

that does make a lot of sense and is structurly easier to read, but when i use that the method always returns the empty string '', where before it just only worked for a single word.
I only used the method i had because it was the most reliable example i had at the time, and i understood how to use it, just didn't know if there was a beter way really.
Was This Post Helpful? 0
  • +
  • -

#6 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: handling a specific input

Posted 11 March 2007 - 11:49 AM

Are you using the old cgi-lib.pm module for parsing form data? If so you really should be using the CGI module instead. cgi-lib.pm is very old and should no longer be used. The CGI module has some legacy support for the common cgi-lib functions.

This regexp will only return the first "word" in $in{'comments'} because the \w class does not include spaces.

my ($comments) = ($in{'comments'} =~ /^\s*(\w+)\s*$/);


what you probably want to do is this:

my $comments = $in{'comments'};
$comments =~ s/^\s*//;
$comments =~ s/\s*$//;
return $comments; 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1