jeckyhl has asked for the wisdom of the Perl Monks concerning the following question:
hi monks,
I have a small perl6 program that basically rename all files in a directory, according to a given regex
I stuggled some time to get it work because I was using the match variable ($/) to do the filename replacement whereas I should have used the topic variable ($_)
This seems to me very conter-intuitive however. Could someone explain why I can't use $/ in this program ?
here's the code of the original version (doesn't works but if you replace $/[0] by $_[0] it magically works!)
#!/usr/bin/perl use v6; my Int $files_renames_count = 0; for dir(".") -> IO::Path $current_file { my Str $cur_filename = $current_file.basename; next if $cur_filename.IO ~~ :d; #skip directories (see http://doc. +perl6.org/type/IO) my Str $new_filename = $cur_filename.subst( /(.+)\.txt/, { $/[0] ~ " (2).txt" }, # doesn't works :( $/[0] is always +empty # works with $_[0] however (???) :i # case insensitive replacement ); if ($new_filename ne $cur_filename) { if ($new_filename.IO ~~ :e) { say "skipping file $cur_filename : $new_filename already e +xists"; } else { say "renaming $cur_filename in $new_filename"; rename $cur_filename, $new_filename; $files_renames_count++; } } } say "$files_renames_count files renammed";
i use perl6 version 2014.08-284-gfcdf308 built on MoarVM version 2014.08-108-g93d47dd
my OS is Windows7
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: perl6 match variable
by Anonymous Monk on Sep 09, 2014 at 14:14 UTC | |
by jeckyhl (Novice) on Sep 09, 2014 at 21:30 UTC | |
by grondilu (Friar) on Sep 10, 2014 at 09:56 UTC | |
by jeckyhl (Novice) on Sep 10, 2014 at 21:32 UTC | |
|
Re: perl6 match variable
by raiph (Deacon) on Sep 17, 2014 at 00:22 UTC | |
by jeckyhl (Novice) on Nov 23, 2015 at 09:48 UTC | |
by raiph (Deacon) on Nov 24, 2015 at 04:18 UTC | |
by jeckyhl (Novice) on Sep 18, 2014 at 13:08 UTC |