Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3279140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:33:33+00:00 2026-05-17T19:33:33+00:00

I have two scripts in which I’m experimenting with CSV_XS. In the first, I

  • 0

I have two scripts in which I’m experimenting with CSV_XS. In the first, I hard-coded everything: source directory, filename, and the csv delimiter I wanted to look for. The script works great. In the second, however, I try to dynamically discover as much as possible. That script seems to run, but it outputs nothing.

I’m having trouble figuring out why, and I was hoping you fine Perl folks wouldn’t mind lending a second set of eyes to the problem:

First, the successful script:

#!/usr/bin/perl -w
use Text::CSV_XS;
my @records;
my $file = 'Data/space.txt';
my $csv=Text::CSV_XS->new({ sep_char => " " });

open(FILE,$file) || die "Couldn't open $file: $!\n";
while (<FILE>){
 $csv->parse($_);
 push(@records,[$csv->fields]);
}
close FILE;

foreach (@records){
 print $_->[0], ",", $_->[1], ",", $_->[2], ",", $_->[3], ",", $_->[4], "\n";
}

And second, the “failing” script:

#!/usr/bin/perl -w
use Text::CSV_XS;

$input_dir = $ARGV[0]; #I pass "Data" on the command line
my @records;

opendir(DIR, $input_dir) || die "cannot open dir $input_dir: $!";
my @filelist = grep {$_ ne '.' && $_ ne '..'} readdir DIR;
closedir DIR;

foreach $file (@filelist){
 print "Input file='",$input_dir,"/",$file,"'\n";
 if ($file =~ /comma/) {$sep=','}
    elsif ($file =~ /pipe/) {$sep='|'}
    elsif ($file =~ /space/) {$sep=' '}
    else {die "Cannot identify separator in $file: $!";}
 print "Delimiter='",$sep,"'\n";   
 open(FILE,$input_dir||"/"||$file) || die "Couldn't open $file: $!\n";
 my $csv=Text::CSV_XS->new({ sep_char => $sep });
 while (<FILE>){
  $csv->parse( $_ );
     push(@records,[$csv->fields]);
  print "File Input Line:'", $_ ,$csv->fields,"'\n";
 };
 close FILE;
}

foreach $record (@records){
 print $record->[0], ",", $record->[1], ",", $record->[2], ",", $record->[3], ",", $record->[4], "\n";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:33:33+00:00Added an answer on May 17, 2026 at 7:33 pm

    This line looks kind of suspect:

    open(FILE,$input_dir||"/"||$file) || die "Couldn't open $file: $!\n";
    

    I don’t think you want to put those || in there. What that does is check to see if $input_dir is true, then if it isn’t, it check to see if "/" is true (which it always is). Your $input_dir is likely always true, so you’re just opening the $input_dir.

    You should be using File::Spec to create your fully-qualified files:

    my $fullfile = File::Spec->catfile( $input_dir, $file );
    open( FILE, $fullfile ) || die "Couldn't open $fullfile: $!\n";
    

    This will “do the right thing” in putting a / where appropriate (or, if you’re on Windows, \). Then pass that in to your open() command.

    Further, you should be using lexical filehandles and directory handles, along with the three-option open():

    open my $fh, '<', $fullfile or die "Could not open file $fullfile: $!\n";
    

    Lexical filehandles are much safer, as they can’t get overridden by some other module defining a FILE filehandle. Three-option open() is easier to understand and isn’t prone to error when you have a filename that has a > or < or | in it.

    If you want to get really crazy, put use autodie; at the top, so you don’t even have to check for the return value of open() or opendir():

    use autodie;
    open my $fh, '<', $fullfile;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two SQL scripts which get called within a loop that accept a
I have two scripts which use Mechanize to fetch a Google index page. I
I have two scripts parent.sh and child.sh. There is a variable in parent.sh which
I have two scripts that often need to be run with the same parameter:
I have two PHP scripts, both using the same session by calling session_name('MySessID') .
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have few SQL scripts which setups the db of an app. have few
I have two Python scripts in two different locations and cannot be moved. What
I have two greasemonkey scripts loading on a particular url. How do I make
I am trying to compare two decimal values in Java script. I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.