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