in reply to No grokkage on regex search/replace

First, your code shouldn't even compile; your s/// operator doesn't have a terminating '/' character. Be sure to paste actual code snippets wherever possible.

Now for the analysis:

One problem is that metacharacter-classes and quantifiers are meaningless on the right-side (the replacement side) of an s/// operator. You can't say, "s/\s+(\w+)/$1\s+/;"

That should be written something more along the lines of:

# Wrong: $phr =~ s/\s+(w+)\s+(w+)/$2\s+$1; $phr =~ s/\b(\w+)(\s+)(\w+)\b/$3$2$1/;

Also, I really do think you're doing yourself a disservice by not picking up a copy of the Llama book, "Learning Perl." That is the introductory course of choice. It just seems like your code has a lot of non-perlish nuggets. For example, what is your initargs() subroutine supposed to be doing? And why is it passing variables around through osmosis instead of through proper parameter lists and return values?

And don't forget:

use warnings; use diagnostics;

.... at least while learning.

Update: One more thing... Please be careful to learn about security issues if you plan to place an online CGI script that deals with money, takes user input, etc. I really think that's too important to gloss over in your first 21 days of Perl programming. We've recently seen what happened to Nik, who ignored warnings about CGI security concerns. Putting a script where others will be executing it (such as CGI) is not something to be done carelessly.


Dave

Replies are listed 'Best First'.
Okay! Okay! You Talked Me Into It!!! :)
by bluethundr (Pilgrim) on May 25, 2004 at 02:29 UTC
    Also, I really do think you're doing yourself a disservice by not picking up a copy of the Llama book, "Learning Perl." That is the introductory course of choice. It just seems like your code has a lot of non-perlish nuggets. For example, what is your initargs() subroutine supposed to be doing? And why is it passing variables around through osmosis instead of through proper parameter lists and return values?

    Thanks for the pointers! Very useful indeed! As to the points that I was "ignoring" from other quite useful posts...for some points and concepts (though I decline to go into specifics here, more in the interest of brevity than any sense of peevishness that I don't in fact harbor. and though you'd have no way of knowing this was merely because of the extremely early stages of this script) I was in fact purposefully doing so in the interests of getting a script off the ground when I was really shooting to get a tighter grip regexing. Whenever learning a new language, sometimes a blank vi terminal looms like a blank canvas for even the simplest tasks.

    I'm a not terrible (though certainly not museum quality...maybe in my old age...) artist some of the time. Picking up a piece of chalk or a paintbrush to a blank sheet of paper or taking a knife to a bit of clay is a minor act of courage. I'm sure that feeling doesn't go away once you've deveoped great facility with a language and you have an ambitious project. But for these "baby steps" I'm taking here publicly on the web even something minor as "reverse a phrase" is a head-scratcher.

    Thanks for the reminder about the "reverse" function, by the by. For some reason that one slipped my mind! But I was finally convinced of the sisyphean nature of my struggles when I tried to "use strict;" as everyone here had been screaming at the top of their lungs (advisedly so, it would seem! huh! go figure! trust the monks!) for me to do and I couldn't even assign a variable in such a way that it would compile! "Oh shit!": says I. Not allowing myself to say (perhaps more appropriately) "whoa is me" and ordered the llama book quicker than you could say get thee to a nunnery!" Even ordered it for same day delivery (as I very luckily work in the center of the whole damnable universe! ;) but sadly it failed to arrive in time for me to start absorbing (devouring? might actually be a better word?) during my hour+ train commute home. It'll be there tomorrow (godwilling) so I can start...again...from.........SCRATCH!!!!! AAAARRRRGH!!!!! DAMN YOU LAURA LEMAY!!! YOU WANTON TROLLOP!!!...you...IMPUDENT STRUMPET!!!...harumph...grumble...

    Seriously, though I bear no will of personal ill tidings towards the woman Laura Lemay herself, if Sams is still publishing that book they should stop. Immediately. It's about the same vintage as the Llama book. And if I'm more than half way through it and I have yet to see word one about the use of strict off to the dustbin it goes! I've always had the best of luck with O'Reilly. If I weren't already having my head examined, I'd say "I should have my head examined" for looking anywhere else! pheh!