Although a few others have posted good information already, I'll toss in my two cents.

And yes, I am aware that I'm not using strict or warnings, and that I should be, but...

Speaking from personal experience, I'd recommend getting into the habit of using these lines of code, which will help you identify problems sooner.

Also, I noticed that you only error checked on just one of the 3 files that you opened, but you didn't print out the $! variable that contains detailed information about why the open command fails. If you add that in every time you open a file, it will help to quickly identify the source of problems if open fails.

Applying the above thoughts along with the stuff from jdporter's and blakew's posts, I'd rewrite your code as the following untested code:

use strict; use warnings; open(FILE,"<messages.txt") or die "Unable to open file 'messages.txt': + $!\n"; open(my $daniel,">Daniel.txt") or die "Unable to open file 'Daniel.txt +': $!\n"; open(my $steffi,">Steffi.txt") or die "Unable to open file 'Steffi.txt +': $!\n"; my $fh = "none"; while (<FILE>) { chomp; if (/^\s*Daniel\s+Lastname\s*$/s) {$fh = $daniel;} if (/^\s*Steffi\s+Lastname\s*$/s) {$fh = $steffi;} if ($fh ne "none") {print $fh $_;} } close($steffi); close($daniel); close(FILE);

Hopefully the code above or from blakew's post will help get you going the right direction.


In reply to Re: Working with text files, thinking my issue is with variables inside if loops by dasgar
in thread Working with text files, thinking my issue is with variables inside if loops by Steffi

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.