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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
= ~m/^\w*$/

 



Baris
Novice

Jun 10, 2002, 10:44 AM

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

Whats this doing?


Code
  

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


Thanks.


fashimpaur
User / Moderator

Jun 10, 2002, 11:02 AM

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

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

'^': 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
written:

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

Hope this was what you were looking for,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Baris
Novice

Jun 10, 2002, 11:17 AM

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

Thanks Dennis.


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

$sw = '-F';

}



...



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



Do you know maybe why here -F is used?



fashimpaur
User / Moderator

Jun 10, 2002, 11:36 AM

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

Baris,

From the grep manpage:


Quote


-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,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
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