Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Writing to an output file

by ameezys (Acolyte)
on Apr 17, 2019 at 20:57 UTC ( [id://1232729]=perlquestion: print w/replies, xml ) Need Help??

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

my $filename = 'c:\Users\a\Desktop\b\c\test_3.v'; open(FH, '<', $filename) or die "Format of bench file name: circuit_na +me.v \n example : s1423.v\n"; while(<FH>) { print "Hello"; } print "Hello too"; close(FH);
I am currently reading an input file. However I want to print out everything inside and outside the while loop into a new output text file. Ex: output.txt. How do I get all the printing to write into a new file? Ive been reading and did some search on the internet but still couldn't figure it out.

Replies are listed 'Best First'.
Re: Writing to an output file
by haukex (Archbishop) on Apr 17, 2019 at 21:15 UTC

    I would recommend you read through perlintro, it's not very long and it shows how to do basic file I/O, for example:

    my $filename = "output.txt"; open my $fh, '>', $filename or die "$filename: $!"; print $fh "Hello"; close $fh;
Re: Writing to an output file
by 1nickt (Canon) on Apr 17, 2019 at 21:18 UTC

    Hi,

    open(FH, '<', $filename) or die ...

    Better written as

    open(my $IN_FH, '<', $filename) or die ...; while (<$IN_FH>) { ... }

    So, if "<" is the second arg for when you want to read from a file ...

    See (please, for goodness' sake, see) https://perldoc.perl.org/perlintro.html#Files-and-I%2fO


    The way forward always starts with a minimal test.
Re: Writing to an output file
by AnomalousMonk (Archbishop) on Apr 17, 2019 at 21:51 UTC

    Ive been reading and did some search on the internet ...
    I find that hard to believe.
    ... but still couldn't figure it out.
    That I can believe.

    In addition to the links given by others, please see the articles in the Monastery's Tutorials -> Input and Output section, open, and perlopentut. (Update: And see also the autodie pragma.)


    Give a man a fish:  <%-{-{-{-<

Re: Writing to an output file
by hippo (Bishop) on Apr 18, 2019 at 08:50 UTC
    How do I get all the printing to write into a new file?

    See select.

      See select.

      A very good point regarding the "all the printing" part of the question! ameezys: Note that the effect of select is global, so it has the potential of affecting code in modules that you call after the select, and if you want to resume printing to STDOUT after writing the file, you'll have to store the return value of the first select to use for restoring the output handle later. Note you can always print to a specific filehandle, e.g. print {$filehandle} $output; or print STDOUT $output;.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1232729]
Approved by haukex
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found