Hi all,

coming home and seeing that this thread is growing I really get curious about what the problem is. 1straw seems to be a nice guy. So, I took the corrected program, created the directories addressed and put the data to the files.

After the first run I see the same weird result as 1straw.

So I follow the advice of davido who was giving the hint that it would be intersting to see what is slurped by the first getline in the relevant function. Inserted a use Data::Dumper; to the friends at the beginning of the script and changed the line

$csv ->getline ($fh);

into

my $header = $csv ->getline ($fh); print Dumper(\$header), "\n";

Another run showed the following output:

$VAR1 = \[ '# de tienda', ' tipo', ' ubicacion', 'li', 'location1', 'li', 'location2', 'li', 'location3', 'li', 'location4', 'li', 'location5', 'li', 'location6', 'li', 'location7', 'ff', 'location8', 'ff', 'location9', 'ff', 'location10', 'li', 'location11', 'ff', 'location12', 'ff', 'location13', 'ff', 'location14', 'ff', 'location15', 'li', 'location16', 'li', 'location17', 'ff', 'location18', 'ff', 'location19', 'ff', 'location20', 'ff', 'location21', 'li', 'location22', 'li', 'location23', 'ff', 'location24', 'ff', 'location25', 'ff', 'location26', 'ff', 'location27' ];

Uuuuuppsss, what is that? The whole file is slurped as one line. How can this be? What is the line seperator? Oh, there is no explicit definition! So, what is taken? I looked at the docs: Ahhh, the default seems to be the special variable $/. Oh, I've seen that variable in the script. Looking around I found this code in open_po:

open ($po_data, '<', $edi_file) or die "Could not open the file '$ed +i_file' $!\n"; undef $/; @po_array = split (/\~/, <$po_data>);

Oh, oh, you've changed a GLOBAL variable which changes the semantics of Text::CSV. (Search for "Global variables are bad" in Perlmonks ;-) )

So, a simple proove if this is the culprit. Put a local in front of it:

local $/ = undef;

and the next run shows that it's found.

Conclusion: davido gave the right hint but you seem not to put a print statement to the header gathering line which gave definitely the right hint. Changing global variables without localisation is risky. Don't localize, use CPAN modules doing what you like, e.g. File::Slurp. Use Data::Dumper as often as possible when you want to print debug lines. You're freed from thinking about what is stored to a variable.

Hope, we all could help you.

UPDATE: Argggghhh, this is the problem here: When you write a long answer, it can happen that someone is answering meanwhile. After looking at the results of my post I've seen that the right hints were given meanwhile. So, a ++ to those who were faster. :-)

Best regards
McA


In reply to Re: Cannot work on second file after reading first file. by McA
in thread Cannot work on second file after reading first file. by 1straw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.