Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: simple program to write a file from commandline

by druthb (Beadle)
on Mar 25, 2012 at 17:51 UTC ( #961512=note: print w/replies, xml ) Need Help??


in reply to simple program to write a file from commandline

Those tutorials and resources are all great, and I heartily agree that you should spend some time with them; however, here's one possible answer to your immediate issue:

use strict; #These two lines are almost-always use warnings; #a good idea print "Enter your data.\n"; print "Type .. at the beginning of a line to quit.\n"; open my $output_file,'>','output_file.txt' or die "We got trouble here!\n"; LOOP: while (<STDIN>) { my $data = $_; chomp $data; last LOOP if ($data =~ m/^\.\./); print $output_file "$data\n"; } close $output_file; exit;

One of Perl's prime directives is that There Is More Than One Way To Do It. You could do this in fewer lines, sure, and there are many sorts of Perl idioms that would be shorter/more efficient/more elegant. But this can get you started. Welcome to Perl, and to PerlMonks!

UPDATE: added check for failed open(). There is more than one way to do this too: use autodie is what I usually do. Thanks, Happy-the-monk, for the reminder!

D Ruth Bavousett

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-12-02 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (37 votes). Check out past polls.

    Notices?