Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Perl regular expression question help

Posted on 2003-03-24
6
Medium Priority
?
219 Views
Last Modified: 2010-03-05
Ok I'm new programmer to Perl and all that regular expressin stuff isn't going well with me.  Basically I'm trying to understand the following 2 lines of code:

($0=~ m,(.*)/[^/]+,)   && unshift (@INC, "$1");
($0=~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");

Can someone please explain what that regular expression is comparing to?  Is that m there the match keyword?  Isnt the format for match m//?  I'm confused!  What is been put in the array @INC?  
0
Comment
Question by:regulatorz
6 Comments
 
LVL 3

Expert Comment

by:prady_21
ID: 8200467
First of all
m// is the matching operator as you know, but perl allows any kind of specifiers, for ex m, ,  is also a matching specifier, and also m. .  
http://webdevelopment.developersnetwork.com/Articles.asp?Article=143


And for your next question, when we do matching like
m// or m,, or anything of that sort, then anything that is specified in the brackets ie () is assigned the value $1.
ie m/(.*)/ puts the value that matches (.*) into $1.
see ->
http://www.comp.leeds.ac.uk/Perl/sandtr.html
0
 

Author Comment

by:regulatorz
ID: 8200816
Ok so for this one say if $0 has "/test/blah.pl" then $1 will have "/test" or empty?
0
 
LVL 3

Expert Comment

by:prady_21
ID: 8201966
The first expr matches /test ($1 = "/test"
and i think the second expr is used for filenames of the type \test\blah.pl


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Accepted Solution

by:
PC_User321 earned 80 total points
ID: 8202007
The first regular expression is searching $0 for:
    Any number of any characters, then
    /, then
    one or more characters excluding slashes
If this is found then the first character string is stored in $1 and is added to the front of the @INC array.

The first regular expression is similar, except it uses backslashes instead of slashes.

To observe the operation, run this script:

($0=~ m,(.*)/[^/]+,)   && unshift (@INC, "$1");
($0=~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");
print join "\n", @INC;
0
 
LVL 6

Expert Comment

by:holli
ID: 8205906
some notes on regex.
once you see .* in a regex, hold on and think how you can avoid it. that is because .* can very easily cause heavy backtracking what makes your regex slow.

a better way (and a non-platform-specific one) for your code above is this:

BEGIN { use File::Basename; unshift ( @INC, (fileparse($0))[1] ); }

holli

0
 

Author Comment

by:regulatorz
ID: 8207009
thanks for all the help.  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question