barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

Given the following code.
my $link = "err"; if ($q->param('title') == "Political Animal") { my $link = "pa"; } elsif ($q->param('title') == "Newslink") { my $link = "nl"; } elsif ($q->param('title') == "New Slang") { my $link = "ns"; }elsif ($q->param('title') == "Rant With Ryan") { my $link = "rwr"; } elsif ($q->param('title') == "Planet Venus") { my $link = "pv"; } elsif ($q->param('title') == "All Day Breakfast") { my $link = "adb"; } else { my $link = "err"; } # add item (tech. replace) LSRfm::Database::Podcasts->create({'title' => $q->param('title'), 'description' => $q->param('de +scription' 'short_description' => $q->par +am('descri 'picture' => $q->param('pictur +e'), 'alt' => $q->param('alt'), 'updated' => Class::Date->now( +), 'link' => $link });

and say $q->param('title') is Polictical Animal, why is it not validating as a truth? and thus Im getting $link=err?

Yours

Barry Carlyon

Replies are listed 'Best First'.
Re: Help with an IF?
by Melly (Chaplain) on Apr 05, 2006 at 10:08 UTC

    Firstly, "==" is numeric comparison - use "eq" for string comparison. I'm also assuming that title isn't "Polictical Animal", which is never going to match "Political Animal", however you handle the comparison... ;)

    As an extra, this looks like a messy way to handle this sort of case.

    Try (untested):

    %linkhash = ( 'Political Animal' => 'pa', 'Newslink' => 'nw' ); if(defined $linkhash{$q->param('title')}){ $link = $linkhash{$q->param('title')}; } else{ $link = 'err'; }
    Tom Melly, tom@tomandlu.co.uk
Re: Help with an IF?
by bart (Canon) on Apr 05, 2006 at 10:14 UTC
    Perl is not PHP, Perl has different comparison operators for strings and for numbers. That is because Perl doesn't really let you distinguish between whether a value is a string or a number. So, is "0" == "00"? Perl resolves that ambiguity by explicitely making you treat the scalars as strings, or as numbers. == is for numbers, eq is for strings. See perlop.

    Oh, and you have one more major problem: you're declaring your $link variables every time inside the code blocks. That limits their scope to that block, so there's no way you can access these variables outside those blocks. What you have, is a bunch of independent variables all called $link.

    What you need is one declaration, at the top. You have that. Just drop the my on every other line that refers to $link. Like this:

    my $link = "err"; if ($q->param('title') eq "Political Animal") { $link = "pa"; } ...
Re: Help with an IF?
by McDarren (Abbot) on Apr 05, 2006 at 10:13 UTC
    why is it not validating as a truth?
    Because you are using a numerical comparision operator (==) to compare two strings. What you want is "eq"

    See perldoc perlop

    Now, if you don't mind me saying so.. that big series of if/elsif's really smells!

    If it were me, I would refactor that into something like so:

    my $title = 'Political Animal'; # Or in your case, $q->param('title') my %links_to = ( "Political Animal" => "pa", "Newslink" => "nl", "New Slang" => "ns", "Rant With Ryan" => "rwr", "Planet Venus" => "pv", "All Day Breakfast" => "adb", ); my $link = $links_to{$title}; print $link;
    Cheers,
    Darren :)
Re: Help with an IF?
by Samy_rio (Vicar) on Apr 05, 2006 at 10:01 UTC

    Hi, use eq instead of ==

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';