Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

please explain (regular expression)

by Anonymous Monk
on May 21, 2003 at 20:13 UTC ( #259865=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi everyone can anyone tell me what this line is doing i am so confused
my $k; if (defined($dir)) { $back_one = $1 if $dir =~/(.*)\/.*$/;
thanks alot

Edit by tye, title, add CODE tags

Replies are listed 'Best First'.
Re: please explain
by DigitalKitty (Parson) on May 21, 2003 at 20:35 UTC
    Hi Anonymous Monk,

    my $k; if (defined($dir)) { $back_one = $1 if $dir =~/(.*)\/.*$/;

    $k is a lexical variable. It's scope ( where it can be seen ) is limited to 'main' ( outside of all subroutines ) unless it has been passed as a parameter.
    $dir is being analyzed for it's value. If it isn't 'undef', then search through $dir looking for a match of zero or more characters ( .* ) followed by a '\' then zero or more characters on the end of the string denoted by the '$'. The first pattern is enclosed in parentheses. This will store the match in $1. If there were other parentheses, they would store their matches in $2, $3, etc. $1 is then assigned to $back_one.

    Hope this helps,
Re: please explain
by Flame (Deacon) on May 21, 2003 at 20:22 UTC
    First it declares and scopes $k. Then it asks if $dir has a value (in other words, it's not undef). If that is true it then tries to capture everything up to a slash (careful, this looks unpredictable to me, might pass over multiple /'s) and place it into $1. If that succedes, it sets $back_one to $1.

    Hope this helps.

    My code doesn't have bugs, it just develops random features.

    Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Re: please explain
by djantzen (Priest) on May 21, 2003 at 20:19 UTC

    If $dir is defined, see if it ends with "something/something" ('.' means "anything", '*' means "zero or more"). If so, set $back_one to the value on the left side of the '/'. Presumeably this is for some kind of directory traversal code.

    "The dead do not recognize context" -- Kai, Lexx
Re: please explain (regular expression)
by PodMaster (Abbot) on May 22, 2003 at 07:42 UTC
    Whenever you come upon a regex you don't understand, try YAPE::Regex::Explain on for size
    use YAPE::Regex::Explain; die YAPE::Regex::Explain->new(qr/(.*)\/.*$/)->explain; __END__ The regular expression: (?-imsx:(.*)/.*$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://259865]
Approved by Enlil
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2018-01-16 19:38 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (188 votes). Check out past polls.