What do you think about this? I like to enclose my file I/O functions in subs, so I don't even have to deal with opening files and stuff like that. Call me lazy. LOL

#!/usr/bin/perl -w use strict; use warnings; ######################################## my $INPUT_FILE_NAME; my $OUTPUT_FILE_NAME; Init(); my @LINES = ReadTextFile($INPUT_FILE_NAME); # DO SOMETHING CreateFile($OUTPUT_FILE_NAME, join("\n", @LINES)) or die("Can't write to file...\n\n"); print "SUCCESS!\n\n"; exit; ######################################## sub Init { my $SELF = GetFileName($0); $INPUT_FILE_NAME = (@ARGV) ? $ARGV[0] : ''; $INPUT_FILE_NAME or die("\nUsage: $SELF <filename>\n\n"); $OUTPUT_FILE_NAME = $INPUT_FILE_NAME . '.out'; } # Usage: STRING = Trim(STRING) - Removes whitespace, newline character +s and other special characters before and after STRING. Returns a new + string. sub Trim { @_ or return ''; my $T = $_[0]; defined $T or return ''; my + $N = length($T); my $X = 0; my $Y = 0; while ($N--) { if (vec($T, $N +, 8) > 32) { $X = $N; $Y or $Y = $N + 1; } } return substr($T, $X, $Y + - $X); } # Usage: FILE_NAME_ONLY = GetFileName(FULL_NAME) - Returns only the na +me portion of a full file name. sub GetFileName { @_ or return ''; my $W = shift; defined $W or return + ''; length($W) or return ''; $W =~ tr|\\|/|; return substr($W, rinde +x($W, '/') + 1, length($W)); } # Usage: STRING = _FileName(\@_) - Removes the first argument from @_ +just like shift() does and returns a file name. This function does no +t check syntax, but it does remove some illegal characters (<>|*?) fr +om the name that obviously should not occur in a file name. If the fi +le name doesn't contain any valid characters, then returns an empty s +tring. sub _FileName { @_ or return ''; my $N = shift; $N = shift(@$N); defin +ed $N or return ''; length($N) or return ''; my $c; my $j = 0; my $V += 0; for (my $i = 0; $i < length($N); $i++) { $c = vec($N, $i, 8); ne +xt if ($c == 63 || $c == 42 || $c < 32); last if ($c == 60 || $c == 6 +2 || $c == 124); if ($c > 32) { $V = $j + 1; } if ($V) { $i == $j or +vec($N, $j, 8) = $c; $j++; } } return substr($N, 0, $V); } # Usage: ARRAY = ReadTextFile(FILE_NAME, [LIMIT]) - Reads the contents + of a text file and returns the lines in an array. If a second argume +nt is provided, then only the first few lines will be processed. Each + line is trimmed before it is stored. sub ReadTextFile { my @A; my $F = _FileName(\@_); length($F) or return + @A; my $M = @_ ? shift : 99999999; defined $M or return @A; $M or re +turn @A; -f $F or return @A; -s $F or return @A; my $H; my $B; my $i += 0; open $H, "<$F" or return @A; while (my $L = <$H>) { $A[$i++] = T +rim($L); $i < $M or last; } close $H; return @A; } # Usage: STATUS = CreateFile(FILE_NAME, STRING) - Creates and overwrit +es a file. Returns 1 on success or 0 if something went wrong. sub CreateFile { my $F = _FileName(\@_); length($F) or return 0; my $S + = (@_) ? shift : ''; return 0 unless defined $S; open(my $H, ">$F") +or return 0; if (length($S)) { print $H $S or return 0; } close $H or + return 0; return 1; }

In reply to Re: Usage of File Handles by harangzsolt33
in thread Usage of File Handles by catfish1116

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.