CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [Gally] Can someone please explain what this code means?: Edit Log


Aug 8, 2002, 3:21 AM

Views: 6328
Re: [Gally] Can someone please explain what this code means?

i'm new to perl too, but i'll try my best. i don't know what pack does. i also might be wrong about some things Smile


$out->{$fields[$i]} =~ s/([A-F0-9]{2})/pack("C",hex($1))/egix;

i'm pretty sure that $out was created with a class. a class is a form of data more complex than a scalar or array. a class can hold scalars, arrays, any type of data, really, and functions too. you shouldn't need to worry about them for quite a while. the $out->{$fields[$i]} looks at the array in the class "out" called "fields", and chooses the element whose number is equal to $i (if $i is 1, it gets whatever $fields[1] is, etc.).

the next part is a bit more complicated, so i'll take it piece by piece.


$out->{$fields[$i]} =~ s/([A-F0-9]{2})/pack("C",hex($1))/egix;

if you haven't learned about the substitute function already, its basic usage is (more-or-less): s/stringtobereplaced/replacementstring/giemosx; where giemosx are the different parameters you can pass to s///. in this case, the string to be replaced is ([A-F0-9]{2}), which is a regex (or regular expression... there's a whole following/religion to these, and a whole lot to learn about them!). i'm not sure what the inverted question mark represents. the square brackets, when used in a regex, represents a character class. it matches any of the letters inside of it. using a dash between 0 and 9 is just a shorter way of writing 0123456789, likewise writing A-F is a shorter way of writing ABCDEF. the {2} tells it to match anything in the character class right before it twice. no more, no less. if it was {,2}, it would mean at most two. {2,} would mean at least two. {2,5} would mean two to five times. putting this whole pattern into parentheses (called "capturing parentheses" when used in this way) puts what it matches into a special variable called a backreference. it's useful for when you want to rearrange things a little bit but keep what you matched originally in at least a part of it. each additional set of parentheses in the pattern creates an additional backreference, unless they're the kind of parentheses that make it so that no backreference is created, but we won't get into that. so (\w+)(\s+) would put what it matches as "at least one alphanumeric letter/number" (the + means "at least one of the previous") into $1, and however many whitespace characters \s+ matches into $2.

since i dont know what pack does, all i can tell you is that the hex($1) means "convert whatever was the first item in parentheses already matched to hexidecimal".

the parameters e, g, i, and x mean (respectfully) "evaluate the replacement string as an expression" (so it runs the function in the replacement string, or replaces any $some reference with what $some represents), "change all occurrences of the pattern" (it 'll only do it on the first instance it finds if you don't include this), "ignore letter case in the pattern", and "ignore the whitespace in the pattern".

and about the =~ if you're wondering, it's used with operators that need to be evaluated as a regex (sorry if that's imprecise, pros Wink). you use it mostly with tr/// (translate), s/// (substitute), and m// ("match" more or less). i'm sure you'll learn about these soon enough. !~ is the opposite of =~. it matches anything that *isn't* in the following regex. also, a regex is like a basic definition of what a certain match "success" should look like. there can, and often will be multiple matches for one regex.

and one more thing... $1, $2, etc. are used as backreferences mostly when it is *not* in the same pattern, e.g. in another part of the operator, or separated entirely. \1, \2, etc. are used when you want to refer to something in capturing parentheses in the same pattern. for instance, /(\w\w)\1/ matches the pattern "lala" (each \w matches one letter, and the \1 matches exactly what was matched by \w\w this time), but not "lalb".

if you have any questions, feel free to ask. i tried not to make things too confusing Smile


gah i forgot to mention...

$string =~ s/someregex/replacementregex/;

looks for someregex in $string and replaces it with replacementregex. i worded things a little poorly in that part Tongue.

(This post was edited by NuclearClam on Aug 8, 2002, 3:33 AM)

Edit Log:
Post edited by NuclearClam (Novice) on Aug 8, 2002, 3:27 AM
Post edited by NuclearClam (Novice) on Aug 8, 2002, 3:29 AM
Post edited by NuclearClam (Novice) on Aug 8, 2002, 3:30 AM
Post edited by NuclearClam (Novice) on Aug 8, 2002, 3:33 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives