Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Unable to rename the contents of the file using perl?

by finddata (Sexton)
on Mar 14, 2017 at 10:18 UTC ( #1184528=note: print w/replies, xml ) Need Help??


in reply to Re^3: Unable to rename the contents of the file using perl?
in thread Unable to rename the contents of the file using perl?

my problem is with renaming the contents of the files
  • Comment on Re^4: Unable to rename the contents of the file using perl?

Replies are listed 'Best First'.
Re^5: Unable to rename the contents of the file using perl?
by poj (Abbot) on Mar 14, 2017 at 10:33 UTC

    If you run this SSCCE then you see where one problem is

    #!perl use strict; my $map = { one =>1, two =>2, three=>3, }; my ($regex) = map {qr /\b(?:$_)\b/ } join '|', map {quotemeta} keys %$ +map; change_file('test.txt',$map,$regex); sub change_file { my ( $fn, $map, $regex ) = @_; # print $fn; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; my $str = do { local $/; <$fh> }; print $str; 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; #<-- error here should be $$str close $fh; } < #test.txt __DATA__ one two three four five
    poj
Re^5: Unable to rename the contents of the file using perl?
by Corion (Pope) on Mar 14, 2017 at 10:22 UTC

    What do you mean by "renaming the contents of the files"?

    A file has a name and has content. The rename system call changes the name, not the content.

    If you want to change the content, you will need to rewrite the file using open and print.

    Please go back to step one and describe in short, simple English sentences what you have, and what you want, and what steps you think you need to take to get from the one place to the other.

Re^5: Unable to rename the contents of the file using perl?
by marto (Archbishop) on Mar 14, 2017 at 10:23 UTC

    I think you're confusing rename with rewrite.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1184528]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-01-20 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?