1
    $chosenbeer = param('beers');
    $beerprice = `grep "$chosenbeer" beers | gawk '{gsub(/[a-zA-Z\t ]/,"");print $1}'`;

This is the code I'm trying to use, $chosenbeer is the string of a list from a CGI page previously. I'm trying to get the price of the beer selected.

beers ex line:

Dogfish Head 60 Minute IPA      35.96

But for some reason this isn't working. Any idea why? When I print $beerprice nothing comes out.

4

1 回答 1

9

您可以在 Perl 中更好地做到这一点,而无需花费大量精力。应该更快、更灵活、更便携、更容易调试并且不包含大的安全漏洞。

sub find_beer {
    my $beer = shift;

    open my $fh, "beers" or die $!;

    # Take the first beer that matches
    my($line) = grep /\Q$beer/, <$fh>;
    return unless $line;

    # I'm presuming the name and price are separated by a hard tab.
    my($name, $price) = split /\t/, $line;

    return { name => $name, price => $price };
}

my $beer = find_beer("Dogfish Head 60 Minute IPA");
print $beer->{price}, "\n";
于 2012-04-26T03:27:35.963 回答