Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

simple program to write a file from commandline

by javed (Initiate)
on Mar 25, 2012 at 17:03 UTC ( [id://961509]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to perl. how do i write a program that will ask for the text from commandline and will save as some given file
  • Comment on simple program to write a file from commandline

Replies are listed 'Best First'.
Re: simple program to write a file from commandline
by Anonymous Monk on Mar 25, 2012 at 17:08 UTC
Re: simple program to write a file from commandline
by druthb (Beadle) on Mar 25, 2012 at 17:51 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://961509]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found