This isn't the point to your post but I wanted to mention a possible bug I noticed:

my ($new_string, $longest_string) = '';

This probably ought to be my ($new_string, $longest_string) = ('','');, because later you're using $longest_string as a pattern with the s/$longest_string// construct. But being undefined is the least of your worries:

#!usr/bin/env perl use strict; use warnings; use File::Temp qw(tempdir); use File::Spec::Functions qw(catfile); my $dir = tempdir('pmtest_XXXXX', TMPDIR => 1, CLEANUP => 1); my $test_filename = '{a}'; my $full_path = catfile($dir, $test_filename); open my $fh, '>', $full_path or die "Cannot create $full_path: $!\n"; print $fh "Hello world.\n"; close $fh; print "We created $full_path as a file.\n" if -e $full_path && -f _; my $target = "$full_path.extra"; $target =~ s/$full_path//;

...produces...

We created /tmp/pmtest_hmfIn/{a} as a file. Unescaped left brace in regex is passed through in regex; marked by <- +- HERE in m//tmp/pmtest_hmfIn/{ <-- HERE a}/ at mytest.pl line 23.

If you use a part of a path as a regular expression pattern that pattern has all the semantics of a regular expression pattern. You probably want to use quotemeta or \Q, to play it a little safer. Otherwise, you're exposing the regex engine to user input, which should be considered hostile. One could create a path that results in a pattern that either fails to parse, or that parses but has abysmal performance.


Dave


In reply to Re: Any way to simulate a Windows path handling for File::Spec without Windows? by davido
in thread Any way to simulate a Windows path handling for File::Spec without Windows? by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.