Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Correct Regex for reading stock symbol?

by ikegami (Pope)
on Jan 31, 2006 at 18:02 UTC ( #526828=note: print w/replies, xml ) Need Help??

in reply to Correct Regex for reading stock symbol?

I don't know what the problem is, but I can suggest some ways of cleaning up your code.

  • && $stock_symbol ne "" is useless since your regexp will never match an empty string.

  • Also, $stock_symbol = $1; is useless, since you're capturing the entirety of the regexp.

  • Finally, length($stock_symbol) < 11 can be removed by replacing the + in the regexp with {1,11}.

What follows is what you get when you apply the above cleanups:

$stock_symbol = $INPUT->param('stock_symbol'); if (!$stock_symbol =~ /^[-\@\w.]{1,11}\z/) { print "Invalid Symbol!\n"; exit; }

Update: Oops, $ can match before a trailing newline. Changed $ to \z to fix.

Replies are listed 'Best First'.
Re^2: Correct Regex for reading stock symbol?
by martin (Friar) on Jan 31, 2006 at 19:08 UTC
    Also, $stock_symbol = $1; is useless, since you're capturing the entirety of the regexp.
    Wrong. This is one way to untaint $stock_symbol. You can read more about taint checks in perlsec.
      Ah yes. The OP even mentioned tainting. Taint is something I need to use more. The OP really shouldn't be using the same variable name for both the tainted and the untainted variables. Joel wrote an excellent paper on the subject. Admittedly, Joel's language didn't have built in tainting, but I recommend the technique nonetheless.
Re: Correct Regex for reading stock symbol?
by Anonymous Monk on Jan 31, 2006 at 18:11 UTC
    Thanks for all the help. I realized that It may not be that portion of the code thats causing a CGI Timeout. I'm pretty sure its because of the "O.O" part in the symbol "AIDO.OB" thats causing a breakage somewhere in my script. ALl other symbols work fine, and if I try adding a "." anywhere inside a symbol, it will stall. So for ex. IF I tried using "BLAH.SYMB" then it will still stall.

    I guess I will try breaking down my script more and try to find the problem.


      What are you doing with the 'untainted' data?


        I found out where the problem is. I'm combining the symbol with a date generated. The file is supposed to be a stock symbol chart I have stored on my local drive. So I'm using the following code to see if it exists or not:
        my $extx = ".jpg"; my $fx = "2006-01-05"; if( -f "D:/stock_charts/$symbol$fx$extx" ) { $filename = $fx; $com_hash{'num'} = "$filename"; last; } #so the filename should look like "AIDO.OB2006-01-05.jpg"
        I think it has something to do with the proper tainting also. I'm going to try turning taint mode off to see if it stops it from timing out.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://526828]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2021-04-23 17:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found