in reply to Removing unwanted chars from filename.
If you are stripping out all characters from a known set then tr is the way to go for 2 reasons. Firstly, it's lightning fast. Secondly you cannot accidentally construct a pattern of more than a single character. Here is a test to demonstrate.
#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 1; my $in = q/xTest-1 [ ] 'copy'.png /; my $want = 'xTest-1copy.png'; my $have = filter ($in); is $have, $want; sub filter { my $str = shift or return ''; return $str =~ tr/A-Za-z0-9.-//cdr; }
🦛
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Removing unwanted chars from filename.
by AnomalousMonk (Archbishop) on Oct 06, 2022 at 23:13 UTC | |
by hippo (Archbishop) on Oct 07, 2022 at 09:22 UTC | |
by AnomalousMonk (Archbishop) on Oct 08, 2022 at 04:34 UTC |
In Section
Seekers of Perl Wisdom