Perl regular expression question help

Posted on 2003-03-24
Medium Priority
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?  
Question by:regulatorz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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. .  

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

Author Comment

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

Expert Comment

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

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;

Expert Comment

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] ); }



Author Comment

ID: 8207009
thanks for all the help.  

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

770 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