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
In reply to perl6 match variable by jeckyhl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |