Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Working out which file a script is working on

by ezekiel (Pilgrim)
on Jul 10, 2001 at 09:07 UTC ( [id://95238]=perlquestion: print w/replies, xml ) Need Help??

ezekiel has asked for the wisdom of the Perl Monks concerning the following question:

I have a quick and dirty script to check the integrity of several data files. I send a list of files as arguments to the script on the command line e.g

script.perl file1 file2 file3

An outline of the script is:

while (<>) { chomp; # ... verify the line ... die unless ($verified); }

When the program dies, it reports what line it was working on, but not which file the line is contained in. Is there a simple way to extract this information? (I can easily rewrite the script to get a list of filenames and then loop over them, but I was wondering if there is a simpler method.)

Thanks.

Replies are listed 'Best First'.
Re: Working out which file a script is working on
by japhy (Canon) on Jul 10, 2001 at 09:27 UTC
    When processing the @ARGV array with a while (<>) { ... } loop, you can access the filename in $ARGV.

    japhy -- Perl and Regex Hacker
Re: Working out which file a script is working on
by danger (Priest) on Jul 10, 2001 at 09:50 UTC

    As japhy mentions, the $ARGV holds the current filename when reading from either <> or <ARGV>. However, note that the line number in $. (and the one reported from the die function) won't be reset between files unless you explicitly check for the end of files and close the filehandle (using the eof function without parens --- see perlfunc:eof). The basic framework is:

    #!/usr/bin/perl -w use strict; while(<>){ chomp; my $verified = 0; # do verification stuff die "problem in file $ARGV at line $." unless $verified; } continue { close ARGV if eof; }

    Note, you can put the eof test at the bottom of the loop rather than in the continue block, but if it is in the continue block it'll still be checked even if you use next inside your loop.

(crazyinsomniac) Re: Working out which file a script is working on
by crazyinsomniac (Prior) on Jul 10, 2001 at 09:24 UTC
    You have to keep track of the filenames.

    You cannot retrieve a filename from a filehandle (see Is it possible to get a filename from a filehandle? )

    You death needs to be more meaningful.

    In other words, don't just die;

    You should be doing something like (no real reason you shouldn't):

    for $file(@ARGV) { open(FHF,$file) or die("Can't open $file $!"); while(<FHF>) { die "crap $file $!" unless ($verified); } close(FHF); }

    update:
    japhy got his answer from perlop (too much magic for me), and apparently (from perlopI/O Operators):

    The null filehandle <> is special: it can be used to emulate the behav +ior of sed and awk. Input from <> comes either from standard input, o +r from each file listed on the command line. Here's how it works: the + first time <> is evaluated, the @ARGV array is checked, and if it is + empty, $ARGV[0] is set to ``-'', which when opened gives you standar +d input. The @ARGV array is then processed as a list of filenames. Th +e loop while (<>) { ... # code for each line } is equivalent to the following Perl-like pseudo code: unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Log In?
Username:
Password:

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

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

    No recent polls found