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

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.