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

Re: Re: New Line at End File

by Anonymous Monk
on Dec 09, 2002 at 22:44 UTC ( [id://218678]=note: print w/replies, xml ) Need Help??


in reply to Re: New Line at End File
in thread New Line at End File

Thanks,
I think I see whats going on, but if you wouldn't mind, can you explain how this works.

Thanks

Replies are listed 'Best First'.
Re: Re: Re: New Line at End File
by jdporter (Paladin) on Dec 09, 2002 at 23:16 UTC
    Sure!

    local @ARGV = $file_name; This sets up the file to be read in via perl's command-line magic. It has the same effect as if you had specified the file on the command line to perl. Any subsequent read from <> will come from this opened file.
    local $^I = ''; This has the same effect as the -i command-line switch to perl. It means that when a file is opened for reading via the @ARGV magic, then a subsequent print will go to that same file. Hence "edit in place".
    If you'd like to make a backup of the original file, use some value other than the empty string shown here. The string you give will be used as the extension for the backup. E.g. '.bak'
    Tip o' the hat to sauoq.
    local $/; # sluurp This sets $/ to an undefined value, thus turning off automatic per-line input reading. Any subsequent read from <> (in scalar context) will read all the remaining available input, instead of just the next line.
    local $_ = <>; Here, we finally get around to reading the input. Because of the first and third lines above, $_ will get the entire contents of the named file.
    while ( chomp ) { } Remember that chomp() returns the number of characters chomped off. Since you want to remove any/all newlines at the end of the file, we keep chomping until chomp says it wasn't able to chomp any more.
    print; When all that is done, print the contents of $_ back out to the file, by virtue of $^I being defined.
    Finally, we want to make sure that all those global variables we used aren't munged from the perspective of any other code. (For example, you might have good reason for @ARGV to keep its original values that you passed on the command line.) Therefore, we use local to localize all our changes to the block specified by the curly braces.

    I hope that's clear. If you'd like any further clarification, just ask!

      Thats perfect!

      Thank you very much!

      Does the .bak file need to be removed in the end or does it get removed automatically?
        The .bak file does not get removed automatically.
        If you need it to be removed, then you have to arrange for that yourself.

        jdporter
        The 6th Rule of Perl Club is -- There is no Rule #6.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found