Open file -- make it nicer ?

Posted on 2003-03-04
Medium Priority
Last Modified: 2010-03-05
Hi *

I want my script (Perl on Windows 32b) to receive 2 command line parameters e.g test.pl <filename> <style>
filename is textfile and style is one-digit character.

Here is how I check and open for file :

if (! @ARGV) {
print "Warning : Please provide input file.";

# Open a file
my $f;
open(FH, $f = shift @ARGV) || die "Error -- cannot open : $f!";

# Read style ...

When I run this script it always show the following message if the input file does not exist.

C:\Perl\eg>perl -w test.pl rte 1
Error -- cannot open : rte! at test.pl line 398

How can I avoid the message after (!) ? It means I do not want to see "at test.pl line 398".

Thanks for idea
Question by:asampadeh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8064842
don't assign in the open statement.

try something like:

# Open a file
my $f= shift @ARGV;
open(FH, $f) || die "Error -- cannot open : $f!";

even better, deal with your arguments in one place. how about:

2 > @ARGV and die "Usage: $0 filename style\n";
my( $infile, $style )= @ARGV;

open FH, '<', $infile
  or die "cannot open file: $!";

## ...
LVL 26

Expert Comment

ID: 8065346
To avoid getting the "at test.pl line 398", you need to put a newline into your die.  The following should work:

open(FH, $f = shift @ARGV) || die "Error -- cannot open : $f!\n";

Accepted Solution

Itatsumaki earned 90 total points
ID: 8065922
The guys above are right, so this is a bit of an aside.  I'm a big supporter of explicitly getting your @ARGV values into locals before testing them:

my $infile = $ARGV[0];
my $style  = $ARGV[1];

if (!$infile) {
     print "Life sucks: no infile!\n";
if (!$style) {
     # handle this case here

I would also suggest that you get into the habit of using the file test operators -f (to check if it's a file) and -T (to check if it's a text file).

if (-T $infile) { #do stuff }

Author Comment

ID: 8071146
It is complete and I learn new thing with additional file test. Direct open(FH,$infile) is the culprit at least in my activeperl script.

Thanks all.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question