Hi scarymonster,

Perl is a very flexible and powerful language, even powerful enough to shoot away your own foot, and perl won't complain. But there exists a perl-builtin pragma with name use warnings; which tries to warn you if you write code that might be problematic and to warn you from some errors that may be difficult to be found. If you dont understand what such a warning means, also use diagonstics; which will try to explain why this might be dangerous.

Another very big help is use strict; which may save you from a lot of other - difficult to find - errors. Why you should use strict is a very good node to start with, and Use strict and warnings and 101 reasons to use strict and Why We Use Strict and Use strict warnings and diagnostics.

So it's a good idea to start every program with the following lines:

#! /usr/bin/perl use warnings; use strict; use diagnostics;

Here some stuff for security:

You don't control if the filenames given are really filenames, and not commands that will be executed. Try the following code which will try to open notepad under windows:

use warnings; use strict; my $filename = 'notepad |'; open( my $FH, $filename ) or die "Can't open '$filename': $!\n";

You can easily avoid this if you use the open with three parameters:

use warnings; use strict; my $filename = 'notepad |'; open( my $FH, "<", $filename ) or die $!;

And if you want to open an existing file for reading, it might also be a good idea to check if it is really existing before opening it:

my $filename = 'notepad |'; unless( -f $filename ) { die "Error: file '$filename' is not existing\n"; } # unless

e.g

#! /usr/bin/perl use warnings; use strict; use diagnostics; print "Enter filename to open (text only):\n\n"; my $infile = <STDIN>; chomp $infile; unless( -f $infile ) { die "Error: file '$infile' is not existing\n"; } open( my $INFILE, '<', $infile ) or die "Error: can't open '$infile' for reading: $!\n"; print "Enter filename to SAVE text file as:\n\n"; my $outfile = <STDIN>; chomp $outfile; open( my $OUTFILE, '>', $outfile ) or die "Error: can't open '$outfile' for reading: $!\n"; print qq~Enter string to replace "and" with\n\n~; my $word = <STDIN>; chomp $word; print "\n" x 3; my $count = 0; while( <$INFILE> ) { $count += s/ \band\b /$word/gix; print $OUTFILE $_; } # while close $INFILE or die "Error: can't close '$infile' for reading: $!\n"; close $OUTFILE or die "Error: can't close '$outfile' for writing: $!\n"; print qq~There were $count instances of "and" in '$infile'\n";

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"


In reply to Re: Counting instances of words by strat
in thread Counting instances of words by scarymonster

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.