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('description'
'short_description' => $q->param('descri
'picture' => $q->param('picture'),
'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
Hi, use eq instead of ==
Regards,
Velusamy R.
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';
}
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 [doc://perlop|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,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";
}
...
perlmonks.org content © perlmonks.org and barrycarlyon, bart, McDarren, Melly, Samy_rio
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03