Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Replies are listed 'Best First'.
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 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 '/';

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


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

        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\//;
        Good question...


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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178278]
Approved by dimmesdale
[Corion]: 1nickt: I imagine (heh) that Agile done somewhat right will make the process (and why we postpone things) somewhat more explicit to the outsider, or at least the "who do we ask to postpone X"
[choroba]: Corion ad functional: in Erlang, it's even more like Prolog, as its syntax is very close to Prolog's

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-12-11 12:08 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (290 votes). Check out past polls.