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

Re: appending to html at beginning

by Discipulus (Monsignor)
on Feb 03, 2017 at 08:10 UTC ( #1180935=note: print w/replies, xml ) Need Help??


in reply to appending to html at beginning

Hello Limbomusic and welcome to the monastery and to the wonderful world of Perl!

I know nothing about what you are telling (madlibs) nor I understand the web related part (it seems a very crude type of web interaction) but if you are trying to prepend some text to an already existing file.. no there is no an automatic way to do it, as far as i know.

You need a two steps work. Between different possibilities you can work with a temporary file: you open a tempfile, you write $new_lines to it then you open the existing original file for read and write it's lines to the temp one. Finally you swap files.

Or (if your existing file is not huge) you can avoid the tempfile overwriting the original (a security copy can be handy anyway; if something go bad you can loose everything!). Something like:

use strict; use warnings; my @new_lines = some_function; # something that retrieve new lines my $file_path = 'file_path.ext'; open my $orig_file, '<', $file_path or die "unable to open '$file_path +' for read!"; my @orig_lines = <$orig_file>; close $orig_file; #reopen it wiping the content open my $new_file, '>', $file_path or die "unable to open '$file_path' + for write!"; #print first new get lines and then old ones print $new_file $_ for @new_lines,@orig_lines;

L*

PS this it is discussed in Perl FAQs: append to the beginning of a file.

Also at SO brian_d_foy tells us a bit more detailed answer: prepend to a file.

See also rename to swap files and sysopen and it's constant if you want more granularity on opening only yet existing file.

HtH

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: appending to html at beginning (updated)
by haukex (Abbot) on Feb 03, 2017 at 08:34 UTC

    Hi Discipulus,

    you open a tempfile, you write $new_lines to it then you open the existing original file for read and write it's lines to the temp one. Finally you swap files.

    I was considering posting this, but went with the Tie::File example instead. But since I happened to be working with code that uses the rename method just the other day, I had the basics handy, here it is:

    #!/usr/bin/env perl use warnings; use strict; use File::Temp qw/tempfile/; use File::Basename qw/fileparse/; my $filename = "/tmp/test.html"; my @to_insert = ( '<p>Hello,', 'World! It is '.gmtime(time).' UTC</p>' ); open my $ifh, '<', $filename or die "open $filename: $!"; my ($basename,$path) = fileparse($filename); my ($tfh,$tfn) = tempfile( $basename.'_XXXXXXXXXX', SUFFIX=>'.tmp', DIR=>$path ); #warn "Debug: writing to $tfn\n"; # change the lines of the input file however you wish here my $found; while (<$ifh>) { print $tfh $_; if (/<!--\s*INSERT\s+HERE\s*-->/i) { $found=1; print $tfh "$_\n" for @to_insert; } } close $ifh; close $tfh; chmod(0666&~umask,$tfn) or warn "Couldn't chmod $tfn: $!"; if (!rename($tfn, $filename)) { my $e=$!; unlink($tfn); die "Renaming $tfn to $filename failed: $e"; } #warn "Debug: Renamed $tfn to $filename\n"; die "Marker not found" unless $found;

    For those who don't know, the advantage is that rename is an atomic operation on many filesystems, meaning that on such filesystems, the filename (in this case /tmp/test.txt) will always exist and always point to either the old version or the new version of the file, and never point to an "in-between" version where the file is still being written.

    Update: Forgot that unlink can set $!, so I preserved its value. Update 2: The code now modifies the file in the same way my other example does. Update 3, 2017-02-07: I was just reminded of the issue that files created by File::Temp have 0600 permissions, so I added the chmod to the code above.

    Update 2017-09-20: I provided an updated version of the above code using my new module File::Replace here: [RFC] File::Replace

    Regards,
    -- Hauke D

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1180935]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-06-23 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (125 votes). Check out past polls.

    Notices?