Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Counting instances of words

by strat (Canon)
on May 28, 2007 at 11:38 UTC ( [id://617827]=note: print w/replies, xml ) Need Help??


in reply to Counting instances of words

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"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://617827]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found