Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: substitution in $0

by RollyGuy (Chaplain)
on Oct 02, 2002 at 13:18 UTC ( #202254=note: print w/replies, xml ) Need Help??


in reply to substitution in $0

I believe that I can provide an answer for why seek isn't happening how you would like it to. You are opening the file in append mode (with the ">>"). In this mode, anything you write will be appended to the original file. There is a lot of information gained by typing perldoc -f open at your nearest command prompt. And from what I see, I think that you need "+<" in front of the $0.

Enjoy.

Replies are listed 'Best First'.
Re: Re: substitution in $0
by Helter (Chaplain) on Oct 02, 2002 at 13:40 UTC
    I agree, testing your code you need to either do as rollyguy has suggested, or it does work doing:
    open (PROG, "+>>$0");
    In running your code withthe + in it, I have found your regexp does not work as well.
    Here is my version, and followed by the output:

    #!/usr/bin/perl use strict; use warnings; my $new_value = 10; my $data=45; open (PROG, "+>>$0"); seek (PROG, 0, 0); #not working while (<PROG>) { s/^(my .data\s*=\s*)\d+(\s*;)/$1.$new_value.$2/e; print; } close (PROG);
    Output:
    #!/usr/bin/perl use strict; use warnings; my $new_value = 10; my $data=10; open (PROG, "+>>$0"); seek (PROG, 0, 0); #not working while (<PROG>) { s/^(my .data\s*=\s*)\d+(\s*;)/$1.$new_value.$2/e; print; } close (PROG);

    Note: I'm not writing to the file itself, now that I'm thinking about it you probably want to write a temp file, then copy it over....

    Hope this helps!
      Thanks to all you monks for the precious suggestions you gave to me!
      I know my code is still terrible!!
      #!perl use strict; use warnings; &changemyself(); sub changemyself{ my $new_value = 10; my $data=6666666666666; #next time this value will be 10 open (COPIA, ">copia"); open (PROG, "+>>$0"); seek (PROG, 0, 0); #I don't know why but it's NECESSARY! while (<PROG>) { s/^( my .data\s*=\s*)\d+(\s*;)/$1.$new_value.$2/e; print COPIA; } close (PROG); # close (COPIA); # # the terrible imitation # of the open (COPIA, "<copia"); # three cards game open (PROG, ">$0"); # while (<COPIA>){print PROG}# close (PROG); # close (COPIA); # unlink ("copia"); # }
      Special thank to Helter!
      greetings from roma, lor*
      PS I never noticed the +>> operator!!! l*
        open (PROG, "+>>$0"); seek (PROG, 0, 0); #I don't know why but it's NECESSARY!

        Because  >> moves the file position to end-of-file.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2022-08-12 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?