Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Perl Function Problem

    Hello,

    I am using the following code to test for operators :
    Code:
    sub isOperator
    {
    	($who)=@_;
    	if($who=="+" || $who=="-"|| $who=="*" || $who=="/" || $who=="^")
    	{
    		return(true);
    	}
    	else
    	{
    		return(false);
    	}
    }
    
    $get=<STDIN>;
    if(isOperator($get))
    {
    	print $get;
    }
    The code should return true only when I enter '+', '-', '*', '/' or '^' but the code returns true for anything that I enter.

    I can't figure out what is wrong. Please help!
    Last edited by premshree; 09-21-2002 at 09:03 AM.

  • #2
    New Coder
    Join Date
    Sep 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    == wrong !

    Try instead "==" will write "eq".
    Ok?
    No comments!

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mean instead of using '==' to use 'eq' ?

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, use 'eq' instead of '=='. The former does a string comparison, whilst the second does a numerical comparison, which isn't what you want here.

    Two other points worth noting:

    Firstly, in your function you're writing return (true);. What this will actually do is return the string 'true', and so whilst it will evaluate to true in a boolean context - it means that return (false); will also return true.

    Perl doesn't have any boolean 'true' or 'false'. Instead just use a '1' and a '0' to get the same effect - it's less typing anyway.

    Also you're forgetting to chomp() $get. This means getting rid of the newline (if any) on the end of the variable, which will cause all of your equality tests in the isOperator() function to return false, because "+\n" is not the same as "+". (The \n stands for newline).

    So your script should probably look something like this:
    Code:
    sub isOperator
    {
    	($who) = @_;
    	if($who eq "+" || $who eq "-" || $who eq "*" || $who eq "/" || $who eq "^")
    	{
    		return 1;
    	}
    	else
    	{
    # because nothing is being returned, this will evaluate to false
    		return;
    	}
    }
    
    $get = <STDIN>;
    chomp $get;
    if(isOperator($get))
    {
    	print $get;
    }
    Hope that helps a bit.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yea that worked! Thanks.

  • #6
    New Coder
    Join Date
    Sep 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    :)

    Originally posted by premshree
    Yea that worked! Thanks.
    No comments!

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Yet another problem!

    I have a file print.pl

    Code:
    print '<html><head><title>Welcome</title></head><body>';
    print param('exp');
    print '</body></html>';
    I run the file in the browser with URL as
    http://localhost/cgi-bin/print.pl?exp=Premshree but the parameter 'exp' is not printed.

  • #8
    New Coder
    Join Date
    Sep 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Yet another problem!

    Originally posted by premshree
    I have a file print.pl

    Code:
    print '<html><head><title>Welcome</title></head><body>';
    print param('exp');
    print '</body></html>';
    I run the file in the browser with URL as
    http://localhost/cgi-bin/print.pl?exp=Premshree but the parameter 'exp' is not printed.
    code:

    print $ENV{CONTENT_LENGTH};

    Please, for more information e-mail me (bdenis@list.ru) and I ask to your question!!!
    No comments!

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am just trying to pass a parameter to a Perl file and print it on the browser. I don't want to use any CGI modules.

  • #10
    New Coder
    Join Date
    Sep 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Easy !

    Originally posted by premshree
    I am just trying to pass a parameter to a Perl file and print it on the browser. I don't want to use any CGI modules.
    Sorry:

    print $ENV{QUERY_STRING};

    It write your parametr. It worked?
    No comments!

  • #11
    New Coder
    Join Date
    Sep 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Easy !

    Originally posted by FlashCoder


    Sorry:

    print $ENV{QUERY_STRING};

    It write your parametr. It worked?
    For more information, Please e-mail me the letter in address^
    den86@hotbox.ru or bdenis@list.ru and a will ask on your all questions and problems !!!

    No comments!

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, that printed the query string that I passed. Maybe I'll try to manipulate the query string to get the name-value pair.

  • #13
    New Coder
    Join Date
    Sep 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by premshree
    Yes, that printed the query string that I passed. Maybe I'll try to manipulate the query string to get the name-value pair.
    code:
    ($a, $b)=split(/=/,$ENV{QUERY_STRING});

    print "\$a is ";$a;

    Did I help you?
    No comments!

  • #14
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I used this :

    Code:
    @string=split(/&/,$ENV{QUERY_STRING});
    @exp=split(/=/,$string[0]);
    $expression=$exp[1];
    $expression =~ tr/+/ /;
    $expression =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $expression =~ s/<!--(.|\n)*-->//g;
    @act=split(/=/,$string[1]);
    $action=$act[1];
    It works great!

  • #15
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just out of interest why aren't you using the CGI.pm module?

    It comes as standard with the Perl distribution so you shouldn't have to fiddle about with installing it.

    All you should have to do is:
    Code:
    #!/usr/bin/perl
    use CGI qw(:standard);
    print header;
    print '<html><head><title>Welcome</title></head><body>';
    print param('exp');
    print '</body></html>';


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •