finddata has asked for the wisdom of the Perl Monks concerning the following question:

Bareword found where operator expected at generate2.pl line 30, near " +s/\.config$//r" Global symbol "$base_name" requires explicit package name at generate2 +.pl line 30. syntax error at generate2.pl line 30, near "s/\.config$//r " Global symbol "$base_name" requires explicit package name at generate2 +.pl line 31. Global symbol "$new_name" requires explicit package name at generate2. +pl line 32. Global symbol "$base_name" requires explicit package name at generate2 +.pl line 32. Global symbol "$new_name" requires explicit package name at generate2. +pl line 33. Global symbol "$new_name" requires explicit package name at generate2. +pl line 34. syntax error at generate2.pl line 36, near "}" Execution of generate2.pl aborted due to compilation errors.
#! /usr/bin/env perl #use feature qw(say); use strict; use warnings; use Cwd qw(getcwd); use File::Copy (); my $name_map = read_map( 'sample.txt' ); my ($regex) = map {qr /\b(?:$_)\b/ } join '|', map {quotemeta} keys %$ +name_map; my $top_dir = 'dir'; rename_dirs( $top_dir, $name_map, $regex ); sub rename_dirs { my ( $top_dir, $name_map, $regex ) = @_; opendir (my $dh, $top_dir) or die "Can't open $top_dir: $!"; my $save_dir = getcwd(); chdir $top_dir; while (my $name = readdir $dh) { next if ($name eq '.') or ($name eq '..'); if ( ( -d $name ) && ( exists $name_map->{$name} ) ) { my $new_name = $name_map->{$name}; rename_file_or_dir( $name, $new_name ); $name = $new_name; } elsif ( -f $name ) { if ( my $base_name = $name =~ s/\.config$//r ) { if ( $name_map->{$base_name} ) { my $new_name = $name_map->{$base_name} . '.config' +; rename_file_or_dir( $name, $new_name ); change_file( $new_name, $name_map, $regex ); } } } rename_dirs( $name, $name_map, $regex ) if -d $name; } chdir $save_dir; } sub change_file { my ( $fn, $map, $regex ) = @_; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; my $str = do { local $/; <$fh> }; close $fh; my $num_replacements = $str =~ s/($regex)/$map->{$1}/ge; if ( $num_replacements ) { write_new_file( $fn, \$str ); } } sub write_new_file { my ( $fn, $str ) = @_; open ( my $fh, '>', $fn ) or die "Could not open file '$fn': $!"; print $fh $$str; close $fh; } sub rename_file_or_dir { my ( $name, $new_name ) = @_; File::Copy::move( $name, $new_name ) or die "Could not rename '$name' as '$new_name': $!"; } sub read_map { my ( $fn ) = @_; my %name_map; open( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; while( my $line = <$fh> ) { chomp $line; my @fields = split /:/, $line; if ( @fields == 3 ) { $name_map{$fields[2]} = $fields[1]; } } close $fh; return \%name_map; }

Replies are listed 'Best First'.
Re: The following error occurs for below code.Help me to solve this?
by haukex (Archbishop) on Mar 13, 2017 at 11:40 UTC

    What version of Perl are you running? See the output of perl -v.

    Although I can't reproduce the errors because I don't have an older version of Perl handy at the moment, based on the error messages I suspect it might be the s///r construct, which was added in Perl v5.14.

    Try changing

    my $base_name = $name =~ s/\.config$//r

    to

    (my $base_name = $name) =~ s/\.config$//
Re: The following error occurs for below code.Help me to solve this?
by thanos1983 (Parson) on Mar 13, 2017 at 11:36 UTC

    Hello finddata,

    I execute your code and I get:

    $ perl test.pl Could not open file 'sample.txt': No such file or directory at test_2. +pl line 74.

    We need sample of your file.

    Update: I think your mistake is extra backslash forward slash "/"on your regex.

    Something like that:

    s/\.config$/r

    Similar question: (perl s/this/that/r ==> “Bareword found where operator expected”)

    Update2: Copied from the question above:

    As ruakh wrote, /r is new in perl 5.14. However you can do this in previous versions of perl:

    (my $foo = $bar) =~ s/this/that/;

    Update3: As haukex suggested I think the Perl Version that you are running is the problem.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      backslash "/"

      That's a forward slash.

        Hello Anonymous Monk,

        You are right, I just updated.

        Thanks for noticing.

        Seeking for Perl wisdom...on the process of learning...not there...yet!