Problem with perl

Posted on 2004-08-02
Last Modified: 2010-03-05
I using Debian Linux 3.0 and Perl 5.6.1
I have this text file (the directory name : /home/student) that i want to insert the
keywords into the database.
 Yield Report
Job      : PQFP64    Start Date: 05/31/04
Tray     : Peak-14   Start Time: 14:40:35
Package  : PQFP-64   Sensor Ht : 239.17mils
Operator : CINDY     Run Time  : 16:11:53
Mach S/N : LSI318    Lot       : D4550027
There's a table with 4 columns  in the text file.      

Perl Program
The below program enables me to extract keywords from multiple text file  
and insert the current directory of the files.


use DBI;
use File::Spec;
$directory = File::Spec->curdir();
print "$directorycld\n";

@ARGV = <*.txt>;
my ( $job, $tray, $date, $lot);
my( $dbh, $sth, $query );

$dbh = DBI->connect( 'dbi:mysql:rvsi');
$query = 'insert into test ( job, date, tray, lot, dir ) values ( ?, ?, ?, ? , ?)';
$sth = $dbh->prepare( $query );

while(<>) {
  if( /Job\s*:\s*(\S+)\s*Start Date\s*:\s*(\S+)/ ) {
    $job = $1;
    $date = $2;
    print "$job\n";
    print "$date\n";
  } elsif( /Tray\s*:\s*(\S+)/ ) {
    $tray = $1;
    print "$tray\n";
  } elsif( /Lot\s*:\s*(\S+)/ ) {
    $lot = $1;
    print "$lot\n";
  if( ( $job ne '' ) && ( $date ne '' ) && ( $tray ne '' ) && ( $lot ne '' ) ) {
    # We have a full record

    $sth->execute($job, $date, $tray, $lot, $directory);

    $job = $date = $tray = $lot = '';



The output when the perl program is executed (database):

| no | job    | tray    | lot      | date     | dir  |
| 36 | PQFP65 | Peak-15 | D4550028 | 06/31/04 | .    |
| 37 | PQFP65 | Peak-15 | D4550028 | 07/25/04 | .    |

currently, i face this 2 problems:

1> the program only enable me to insert multiple text files with the filename ending
 with .txt but not all my filename have .txt (eg abc.txt and cde).
2> it is unable to insert the current directory path of the text files into the dir
table of the database (as shown in the output above)

how can i modify my perl program to enable this two function??
Question by:TPoly
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
  • 5
  • 3
  • 2
LVL 20

Expert Comment

ID: 11700565
1> you can add additional strings to the globbing operator you're using. Something like
  @ARGV = <*.txt *.cde>;

2> to better see what the File::Spec::curdir method has given you, you should change the line reading

  print "$directorycld\n";

to read

  print "$directory\n";


Now, as I read the doc, 'curdir' returns "." on UNIX and Linux. And this matches what you are seeing. To get the full pathname, you need to call rel2abs. So change the lines

use File::Spec;
$directory = File::Spec->curdir();

to be

use File::Spec::Functions qw(curdir rel2abs);
$directory = rel2abs( curdir());


Author Comment

ID: 11701003
thanks it works..

however is it possible to include the name of the file in the directory?
And it still could not read files without .txt

Pls help
thank you.
LVL 20

Accepted Solution

jmcg earned 400 total points
ID: 11701192
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?

2> OK, I've been misled by the names given to things. When you call something $directory, I think you mean you want the directory name. It sounds like what you really want is the file's absolute pathname.

It's a trick of the way that the <> operator works that the name of the current file is in the $ARGV variable.

Inside the 'if' statement before you do your $sth->execute, say:

my $fname = "$directory/$ARGV";


my $fname = rel2abs( $ARGV);

then use $fname instead of $directory in the parameter list. I'll leave it up to you to change the column name.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11701535
thanks it works great for the directory path!!!
sorry, but this perl script is still unable to extract
data from those files without the .txt
LVL 20

Expert Comment

ID: 11701686
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?

Author Comment

ID: 11701908
for example i have all this files (all the format of the files are the same):

test1, test2, test3.txt, test4.txt.

the above perl program only allows me to extract the data from test3.txt and test4.txt
i need to extract the data from all 4 files.

btw, what is current glob expression? how to find it?

LVL 84

Expert Comment

ID: 11701979
@ARGV = <test*>;

Author Comment

ID: 11702039
oh ok i get what you mean

but my actual filenames will be like
DF758  KL133  AS122.txt DF098

so do i need to edit @ARGV = <test*>; to something else?

LVL 84

Assisted Solution

ozo earned 100 total points
ID: 11702154
Are there files in the directory you don't want to process?
If not, you might use
  @ARGV = <*>;
otherwise, is there any way to distinguish the files you want from the files you don't want?

Author Comment

ID: 11711552
thanks guys~ :)    

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

729 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