harangzsolt33 has asked for the wisdom of the Perl Monks concerning the following question:
I am a beginner perl programmer. I am trying to write a function getArgsURL() that will extract the parameters from an URL string. I have samples $A, $B, and $C which I would like my function to work on. But every time I run this code, it throws me an error. And I have no clue how or why this error occurs. It says:
Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HERE / at C:\ BIN\PERL\Example.pl line 104.
Press any key to continue . . .
use strict; use warnings; # Samples my $A = "file:///c:/html/testing.html?P1=123&P2=%28%28BLAH+BLAH+BLAH%2 +9%29"; my $B = "http://www.cnn.org/g/ar.shtml?c=123055&s=%28Top+Stories+%29"; my $C = "http://www.something.com/example/article.php?P1=123&P2=%28%28 +DATA+GOES+HERE%29%29%0D%0A#PGTOP"; my @R = getArgsURL($A); foreach my $S (@R) { print "\nR: ", $S; } ############################################### # This function extracts arguments from an URL # string and returns them in pairs. # # Example: @R = getArgsURL("http://www.cnn.org/g/ar.shtml?c=123055&s=% +28Top+Stories+%29#PGTOP"); # R[0] ---> "c" # R[1] ---> "123055" # R[2] ---> "s" # R[3] ---> "(Top Stories)" # sub getArgsURL { my $S = shift; my @OUTPUT; my @X; my $P; splitAB($S, '?'); splitAB($b, '#'); @X = split('&', $a); foreach $S (@X) { $P = index($S, '='); if ($P < 0) { push(@OUTPUT, $S); } push(@OUTPUT, decodeURLstr(substr($S, 0, $P))); push(@OUTPUT, decodeURLstr(substr($S, $P+1))); } return @OUTPUT; } ################################################ # This function works like the split function, # however it will only split STRING into two # chunks at the first occurrence of PATTERN. # The section before the first occurrence of # PATTERN goes into $a, and rest goes into $b. # # (This function has no return value. # It simply changes the values of $a and $b.) # # Usage : splitAB(STRING, PATTERN) # sub splitAB { my $STRING = shift; my $PATTERN = shift; my @OUTPUT = split($PATTERN, $STRING, 2); $a = $b = ''; if (@OUTPUT > 0) { $a = $OUTPUT[0]; } if (@OUTPUT > 1) { $b = $OUTPUT[1]; } } ############################################ # This is the opposite of the escape() function. # sub unescape { my $XX; my $BYTE; my $INPUT = shift; my @OUTPUT; for (my $i = 0; $i < length($INPUT); $i++) { $BYTE = substr($INPUT, $i, 1); if (ord($BYTE) == 37) { $BYTE = ''; $XX = substr($INPUT, $i+1, 2); if (length($XX) == 2) { $i += 2; $BYTE = chr(hex($XX)); } } push(@OUTPUT, $BYTE); } return join("", @OUTPUT); } ################################################ # This function decodes an URL-style string. # Works like the unescape function, however # it will also convert '+' signs to spaces. # sub decodeURLstr { my $S = shift; # return unescape( join(' ', split('+', $S) ) ); }
What on earth is going on? It gives me an error when I call the split() function.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RegExp error PLEASE HELP!
by afoken (Chancellor) on Jul 04, 2016 at 19:44 UTC | |
|
Re: RegExp error PLEASE HELP!
by haukex (Archbishop) on Jul 04, 2016 at 19:56 UTC | |
by harangzsolt33 (Deacon) on Jul 04, 2016 at 20:04 UTC | |
by afoken (Chancellor) on Jul 04, 2016 at 21:26 UTC | |
|
Re: RegExp error PLEASE HELP!
by Marshall (Canon) on Jul 04, 2016 at 20:20 UTC | |
|
Re: RegExp error PLEASE HELP!
by AnomalousMonk (Archbishop) on Jul 04, 2016 at 21:20 UTC | |
|
Re: RegExp error PLEASE HELP!
by Marshall (Canon) on Jul 04, 2016 at 21:57 UTC | |
|
Re: RegExp error PLEASE HELP!
by $h4X4_|=73}{ (Monk) on Jul 05, 2016 at 10:59 UTC | |
|
Re: RegExp error PLEASE HELP!
by Anonymous Monk on Jul 04, 2016 at 19:42 UTC |