Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

search =~ for /'s

by Anonymous Monk
on Jun 29, 2002 at 22:48 UTC ( #178278=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

well lets say
$new = param('home');
Now I wanna check to see if new has a / at the end, if it doesnt then add a /. But it's a url so what if the user uses a \ instead of /.

Comment on search =~ for /'s
Download Code
Re: search =~ for /'s
by Juerd (Abbot) on Jun 29, 2002 at 23:09 UTC

    $new = param('home'); Now I wanna check to see if new has a / at the end

    if ($new !~ m[/$]) {

    if it doesnt then add a /.

    $new .= '/'; }

    But it's a url so what if the user uses a \ instead of /.

    URLs use slashes, not backslashes.

    By the way: $new =~ s[\z(?<!/)][/];.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: search =~ for /'s
by caedes (Pilgrim) on Jun 29, 2002 at 23:12 UTC
    $new =~ s/([^\/])$/$1\//;
    You shouldn't worry about the backslash if $new is a URL because URl's don't end in backslashes. :-)

    -caedes

      Does file://c:\ qualify as a url?

        Good question...

        -caedes

        since file url's (like file://c:\test) also work with forward slashses instead of backslashes (file://c:/test/), how about simply replacing all backslashes with a forward slash, and then use the same regex that has been provided by caedes:
        $new =~ s/\\/\//g; $new =~ s/([^\/])$/$1\//;
Re: search =~ for /'s
by erikharrison (Deacon) on Jun 30, 2002 at 02:32 UTC

    Everyone has advised you to use a regex so far. However, when you know the position in the string to check for, substr is often a better method, as it is clearer and potentially has a lower overhead.

    $new .= '/' unless substr $new, -1 eq '/';
    Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet

      $new .= '/' unless substr $new, -1 eq '/'; can someone explain what -1 does
        see perlman:perlfunc
        # 1st argument given: string to process # 2nd argument given: offset # so here it means regardless of the true length the # last character in the string substr ($new, -1);

        Have a nice day
        All decision is left to your taste
Re: search =~ for /'s
by thor (Priest) on Jul 01, 2002 at 10:38 UTC
    My suggestion would be to add the slash regardless of what they have. In most reasonable operating systems, /home/thor/testdir is the same as /home/thor//testdir. YMMV, though.

    thor

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178278]
Approved by dimmesdale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-07-11 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls