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

In reply to Re: simple program to write a file from commandline by druthb
in thread simple program to write a file from commandline by javed

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.