Link to home
Start Free TrialLog in
Avatar of asampadeh
asampadeh

asked on

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
Avatar of particle
particle

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

## ...
Avatar of wilcoxon
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";
ASKER CERTIFIED SOLUTION
Avatar of Itatsumaki
Itatsumaki

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asampadeh

ASKER

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.