Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: In my perl script one of the variable it is giving an extra dot.

by kcott (Archbishop)
on May 25, 2022 at 14:37 UTC ( [id://11144185]=note: print w/replies, xml ) Need Help??


in reply to In my perl script one of the variable it is giving an extra dot.

Firstly, I agree with others that it would be preferable to deal with the problem at the source, rather than fixing it within your code. It's entirely possible that you'll need to repeat exactly the same fix in multiple scripts, modules or subroutines.

If that's not possible, it may be insufficient to just add a ^ anchor and remove the g option. What about hidden files (e.g. .bashrc) or places where intentionally specifying the current directory is necessary. You may need something closer to

s{^[.](?=/root(?:/|$))}{}

Consider this SSCCE:

#!/usr/bin/env perl use strict; use warnings; use Test::More; my @tests = ( [qw{./root /root}], [qw{./root/ /root/}], [qw{./root/xyz /root/xyz}], [qw{./root_plus ./root_plus}], [qw{./not_root/ ./not_root/}], [qw{./script_not_in_path.pl ./script_not_in_path.pl}], [qw{.hidden .hidden}], ); plan tests => 0+@tests; for my $test (@tests) { my $file = $test->[0]; $file =~ s{^[.](?=/root(?:/|$))}{}; is $file, $test->[1]; }

Output:

1..7 ok 1 ok 2 ok 3 ok 4 ok 5 ok 6 ok 7

That's just an example. Modify the test cases to better reflect your real data.

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144185]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-16 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found