Open file -- make it nicer ?

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
-iman
asampadehAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

particleCommented:
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: $!";

## ...
0
wilcoxonCommented:
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";
0
ItatsumakiCommented:
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";
     exit();
     }
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 }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asampadehAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.