I'm stuck for resources though - I might be looking in the wrong place. Any good URL's I should bookmark ?
Current thing I'm trying to do is to explode some Name=>Value arrays into constituent parts so that I can pass them off to a URL as GET vars.
For example, I have a RAD_REQUEST var that contains numerous values which I can access like:-
$username = $RAD_REQUEST{'User-Name'}; #username="tj"
$password = $RAD_REQUEST{'Password'}; #password="me"
(if anyone is interested I'm playing with a RADIUS server)
I would like to explode those into a URL GET string that looks like:-
?User-Name=tj&Password=me
Which uses the original naming format from the array and with "=<value>" after it.
I found various array functions in PERL on my searching, but nothing for this type of 2D array.
Any pointers?
Many thanks.
I found the "each" directive and built this:-
while ( ($key, $value) = each(%RAD_REQUEST) ) {
$url_get .= $key."=".$value."&";
}
$url_get = substr($url_get, 0, -1); #*
* I also found the Chop() directive, sounds cool (with a name like that I have to find a way to use it!).
Stuff like that is what makes me like a language.
Thanks! Implemented!
How might I clean up a string response that may contain both leading and trailing newlines and whitespace? I found:-
$string =~ s/\n//g;
... but as far as I can understand that would only replace newlines?
$string =~ s/^\s*//;
$string =~ s/\s*$//;
On a side note:
[\s] is a needless use of the character class brackets []. \s is a character class, it doesn't need to be inside of the brackets unless combined with other things to make a character class that includes more than \s.
I'm stuck for resources though - I might be looking in the wrong place. Any good URL's I should bookmark ?
[perldoc.perl.org...]
[perl.org...] (a little old but still relevant)
[hop.perl.plover.com...] (might be too advanced for perl newbies but its a great resource)
[perlmonks.com...] (tons of info, some is old but still relevant)
it's more like a definitive guide than a learning book.
my "intro to perl" text was actually "CGI Programming with Perl".
there are also some pretty good manpages for perl and they are probably all available on perldoc.perl.org.
for example, check out man perltoot [perldoc.perl.org] (SEE ALSO [perldoc.perl.org]) for some light reading.
glad to see you're hooked!
my "intro to perl" text was actually "CGI Programming with Perl".
Do you mean "CGI Programming with Perl 5" by Eric Hermann? Ditto, still have it right here next to the Camel book. :-)
I think the end-of line character discussion reveals one of the most important fundamentals of perl, TMTOWTDI. Turning to the original question, here's another example of TMTOWTDI:
while ( ($key, $value) = each(%RAD_REQUEST) ) {
$url_get .= $key."=".$value."&";
}
You have to be very careful using while on variables stored in memory because in many cases, as long as the program is running the variable will exist, so while never returns false . . . get it? Use while for file or database reads, things you know will always have an ending, and use for or foreach on internal variables:
foreach $key (keys %RAD_REQUEST) {
$url_get .= $key."=".$RAD_REQUEST{$key}.'&';
}
$url_get =~ s/\&\;$//;
<ahem> note the entity . . . programmers are always ragged for outputting crap code that won't validate, so output usable code . . . :-)
Or better yet, don't put the trailing &(amp;) there in the first place . . .
@keys = keys %RAD_REQUEST;
for $i (0..$#keys) {
$url_get .= $keys[$i].'='.$RAD_REQUEST{$keys[$i]};
if ($i < $#keys) { $url_get .= '&'; }
}
An aside,
?User-Name=tj&Password=me
You do know what a terribly bad idea this is, right?
[edited by: rocknbil at 4:15 pm (utc) on Jan. 8, 2009]
You do know what a terribly bad idea this is, right?
I know what a terribly bad idea it would be if I did this in plain-text, if that's what you mean ;)
It's already on HTTPS and the password in this case is actually an MD5 hash, so it's fine. At least, on my understanding that HTTPS does actually encrypt GET vars and not just POST vars?
Thanks for the book links. Amazon just got paid again.
Do you mean "CGI Programming with Perl 5" ...?
Thanks Krugs, although I also need to remove newlines at the beginning of any string.
Newlines at the beginning of a string is not too common but the regexps that were posted will also remove them as \n is part of the \s character class.
If you buy anything from amazon, get the Perl Bookshelf Reference on CD. It has several perl books pretty much for the price of one. Used copies are dirt cheap. My version 3 has:
Perl in a Nutshell, 2nd Edition (more of a reference and overview of many things perl)
By Nathan Patwardhan, Ellen Siever and Stephen Spainhour
Programming Perl, 3rd Edition (the infamous "Camel" book)
By Larry Wall, Tom Christiansen, and Jon Orwant
Learning Perl, 3rd Edition (the "Llama" book sometimes confused with the "Camel" book)
By Randal L. Schwartz and Tom Phoenix
Perl Cookbook (pretty much a crowd favorite)
By Tom Christiansen and Nathan Torkington
Perl and XML (a great resource)
By Erik T. Ray and Jason McIntosh
Perl and LWP (another great resource)
By Sean M. Burke
Mastering Perl/Tk (I don't do Tk myself)
By Steve Lidie and Nancy Walsh
Older versions have some different titles