Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Hey Monks, I need help understanding how to write to a text file

by Anonymous Monk
on Jun 11, 2014 at 17:55 UTC ( [id://1089570]=perlquestion: print w/replies, xml ) Need Help??

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

Okay, so I've been lurking around for quite a while now, I just started a new job and need to learn Perl! So far, it seems quite wonderful. My question: I'm reading a friend's code and trying to understand how to open and write to a text file. I looked up how it is commonly done, and this seems different.

open(DIN, "<$file_name"); while (<DIN>) { next unless /(.*)Avg: (.*) V/; print OUT "$i, $2 \n"; } close(DIN);

sorry if my formatting isn't the greatest, I'm trying I promise! Also, what is "DIN" ?? Even google doesn't seem to be helping me there. thanks a ton <3

Replies are listed 'Best First'.
Re: Hey Monks, I need help understanding how to write to a text file
by neilwatson (Priest) on Jun 11, 2014 at 18:01 UTC
    DIN is a file handle. The syntax is actually depreciated. The more correct way is this:
    use strict; # always use this for better code. use warnings; # always use this for more meaningful error messages. use English; my $din; open( $din, "<", $filename ) or die "Cannot open file, $filename, error: $ERRNO"; while (<$din>) # process file one line at a time { # do stuff hear on each line. } close( $din );

    Neil Watson
    watson-wilson.ca

Re: Hey Monks, I need help understanding how to write to a text file
by Anonymous Monk on Jun 11, 2014 at 18:09 UTC

    This code reads a file. DIN is a filehandle, its name can be chosen arbitrarily. This code is a little "older" style Perl, a more "modern" way to write the same thing would be:

    open my $din, '<', $file_name or die "failed to open $file_name: $!";

    To write to a file, you'd use ">" instead of "<", and then use print to write to the file, as in print $din "some text\n"; Some quick googling brings up this nice tutorial: http://perlmaven.com/writing-to-files-with-perl , and the documentation for open explains that function in detail.

    There are several good books that will help greatly in learning Perl, see for example the "Learning Perl" series here: http://shop.oreilly.com/category/browse-subjects/programming/perl.do . If you don't want to spend any money yet, chromatic's "Modern Perl" is available online for free: http://onyxneon.com/books/modern_perl/

Re: Hey Monks, I need help understanding how to write to a text file
by deejed (Initiate) on Jun 11, 2014 at 18:18 UTC

    Thank you all a ton! I caved and made an account. Guess I'm a monk now.
    Anon: that is the site I found and read while googling, I was trying to understand why my friend had his this way. Found out from him this was NOT working either haha. I shall check out those books!

Re: Hey Monks, I need help understanding how to write to a text file
by vinoth.ree (Monsignor) on Jun 11, 2014 at 18:20 UTC

    Don't you get perldoc open ? in google search ?

    If FILEHANDLE, in your case its DIN is an undefined scalar variable, a new filehandle is autovivified, meaning that the variable is assigned a reference to a newly allocated anonymous filehandle. Otherwise if FILEHANDLE(DIN) is an expression, its value is the real filehandle.

    Why should I use three-argument open ?

    You should use the three-argument version because it protects against files with crazy names. Consider the following example

    my $filename = "<file.txt"; open( INPUTFILE, "< $filename" ) or die "$!";

    This will interpolate as: open( INPUTFILE, "< <file.txt" ) or die "$!"; So its actually open a file named file.txt instead of one named <file.txt.


    All is well

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1089570]
Approved by toolic
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: (4)
As of 2024-04-25 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found