richill has asked for the wisdom of the Perl Monks concerning the following question:

Im not sure about the
 $subdir =~ m{^/}
  • Comment on what does $subdir = "$BASEDIR/$subdir" unless $subdir =~ m{^/}; do
  • Download Code

Replies are listed 'Best First'.
Re: what does $subdir = "$BASEDIR/$subdir" unless $subdir =~ m{^/}; do
by rafl (Friar) on Mar 18, 2006 at 12:02 UTC
    $subdir =~ m{^/};

    returns a true value if $subdir starts with a '/'.

    So the whole statement leaves $subdir as it is, if it already is an absolute path (starting with /) or it prepends a $BASEDIR and stores the result in $subdir again.

    Cheers, Flo

Re: what does $subdir = "$BASEDIR/$subdir" unless $subdir =~ m{^/}; do
by davidrw (Prior) on Mar 18, 2006 at 13:31 UTC
    Another (more robust, platform-indpendent) way would be to use File::Spec:
    use File::Spec; $subdir = File::Spec->rel2abs($subdir, $BASEBIR);
    But anyways, your confusion may come from seeing m{^/} instead of /^\//, which are equivalent. See perlop and perldoc:perlre for the m// operator. It could also be written as m#^/# or m!^/! (or many others). But any way it's written, it is a regex that is checking for a leading slash -- i.e. if the path is absolute or not. IFF that fails (since "unless" reads as "if !") then it prepends a base directory to $subdir. So after that statement $subdir should be an absolute path if it wasn't already.