Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code works but not with use strict...

Status
Not open for further replies.

sdslrn123

Technical User
Jan 17, 2006
78
GB
This code works without the strict function but I was just wondering can anyone work out why it won't work with the "use strict" function?


#!/perl/bin/perl -w


use CGI;
$query = CGI::new();
$text1=$query ->param("intext");
$pattern=$query ->param("pattern");
print $query ->header();

print $query ->p("(1i) The Inputted Text: $text1\n");
print $query ->p("(1ii) The Inputted Pattern: $pattern\n");


($newtext = $text1) =~ s/\s+//g;
print $query ->p("(2i) The Text to be searched with spaces removed: $newtext");

($newpattern = $pattern) =~ s/\s+//g;
print $query ->p("(2ii) The Pattern to be matched for with spaces removed: $newpattern");


if ( $newtext =~ /^\s*$/)
{
print $query ->p("(3i) The text box was found to be BLANK. Please Try Again.");
}
else
{
print $query ->p("(3ii) The text box was NOT found to be blank.");
}

if ( $newpattern =~ /^\s*$/)
{
print $query ->p("(3iii) The pattern box was found to be BLANK. Please Try Again.");
}
else
{
print $query ->p("(3iv) The pattern box was NOT found to be blank.");
}

if ( $newtext =~ /[^AaGgCcTtUu]/)
{
print $query ->p("(4i) Problems have been found with the syntax of the text. Please Check Or Try Again.");
}
else
{
print $query ->p("(4ii) The syntax of the text is Correct.");

}
if ( $newpattern =~ /[^AaGgCcTtUu|\*]/)
{
print $query ->p("(4iii) Problems have been found with the syntax of the pattern. Please Check Or Try Again.");
}
else
{
print $query ->p("(4iv)The syntax of the pattern is Correct.");
}


if ( $newpattern =~ /(\*.){2,}|(\*){2,}|\*$/) #These are the only ways in which two stars can be together
{
print $query ->p("(5i)Minimum of Two stars present:Bad");
}
else
{
print $query ->p("(5ii)Not Minimum of two stars present: Good");
}

$newtext =~ tr/A-Z/a-z/; # convert to lower case $newpattern =~ tr/A-Z/a-z/;
# convert to lower case

if ( $newtext =~ /$newpattern/)
{
print $query ->p("(6i)A Match Was Found");
}
else
{
print $query ->p("(6ii)No match was found");
}

($regex = $newpattern) =~ s/\*(.)/.*[^$1](.*?)/g;
if ($newtext =~ /($regex)(?!.*?$regex)/)
{
print $query->p("(7)The excellent match was found at position $-[0]");
}
 
Code:
use CGI;
$query = CGI::new();
$text1=$query ->param("intext");
$pattern=$query ->param("pattern");

The last 3 lines there are good examples of why strict doesn't work.

In other programming languages, you need to explicitly declare each variable you use before using it. So you'll need to declare all those variables with "my's" the first time you use them, or strict gives the error about it.

Code:
use CGI;
my $query = CGI::new();
my $text1=$query ->param("intext");
my $pattern=$query ->param("pattern");
 
Thank you so much, I declared some more variables and it works. I am so greateful that people like yourself can take the time out to help a programming fool like myself. Cheers!
 
I was just curious is there a code so that in an if/else statment a specific command is incorrect the program quits rather than continues on to the next statement.

Thanks again.
 
Code:
unless ($cmd eq 'ok') {
    die "Invalid command \'$cmd\' attempted!";
}
is one way. Another is to 'die' with a message showing a brief help text.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top