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:
= ~m/^\w*$/



Jun 10, 2002, 10:44 AM

Post #1 of 4 (1679 views)
= ~m/^\w*$/ Can't Post

Whats this doing?


$variable =~ m/^\w*$/


User / Moderator

Jun 10, 2002, 11:02 AM

Post #2 of 4 (1678 views)
Re: [Baris] = ~m/^\w*$/ [In reply to] Can't Post

Well, this is a regular expression and it is looking for a match

'^': from the start of the word

'$': to the end of the word

'*': there are zero or more

'\w': word characters.

Granted, the order of the characters between the starting foward
slash and ending forward slash is different, but this is what it means.

Actually, this makes no sense, because where would you have
a word that is comprised of zero word characters?

So, should the regex have, instead of '*' used '+', it would have checked
for 1 or more word characters. But, then why do this when a simple truth
check works the same (i.e. if ($variable){...}) A scalar string is true when
it is defined and not empty. But, this let's digits, other than a single zero
test as true.

I guess it's validity as a regex depends on what you are testing for.

Also, note that by itself it does nothing. If there are no more regexes
for a while, the code may be using the value of $& ( the match), $` (the prematch), $' (the postmatch) or $1 (the first match that met the requirements).
Otherwise, the next regex will overwrite these variables to what it's match
returned. It really should be being used inside an if statement.

One last point. If the standard '/' delimiters are used to start and end a regex,
the preceding 'm' (match) is not required. The regex could have simply been

$variable =~ /^\w*$/;

Hope this was what you were looking for,

print unpack"u*",pack "h*",$a,"\n\n";


Jun 10, 2002, 11:17 AM

Post #3 of 4 (1676 views)
Re: [fashimpaur] = ~m/^\w*$/ [In reply to] Can't Post

Thanks Dennis.

 ...if ($grepPatt =~ m/^\w*$/) { 

$sw = '-F';



$g = "$GREP -i $sw \"$grepPatt\" $dd/_cache/$sortf.tbl |";

Do you know maybe why here -F is used?

User / Moderator

Jun 10, 2002, 11:36 AM

Post #4 of 4 (1674 views)
Re: [Baris] = ~m/^\w*$/ [In reply to] Can't Post


From the grep manpage:


-F Match using fixed strings. Treat each pat-
tern specified as a string instead of a regu-
lar expression. If an input line contains
any of the patterns as a contiguous sequence
of bytes, the line will be matched. A null
string matches every line. See fgrep(1) for
more information.

It appears that the test now becomes more meaningful. If the string contains
word characters, it wants the grep performed later to use the -F switch to treat
the string stored in $grepPatt as a string and not a regular expression. Sorry I
cannot be more help than that. I am not that grep saavy.

Hope this helps,

print unpack"u*",pack "h*",$a,"\n\n";


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

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