Mr.Churka has asked for the wisdom of the Perl Monks concerning the following question:

update I started on a unix OS and ended up switching to windows halfway through development. I missed the glob as it hadn't caused an issue before. Thanks everyone!

Hello monks, I'm writing a bit of code to dump a catalog into a MySQL 4.1 database. To check the XML for accuracy, I want to dump it into a text file to look it over before dumping it into my database. I've set a series of subs to pull out the useful information, build a hash of hashes to keep the relationships. I then dereference the hashes and print them to a text file.

Here's my code.

use strict; use warnings; use DBI; use xml::twig; use utf8; my $dbh= connect_to_db(); our ($SKUgroupNUMBER, $smallPACKAGE, $sellingpoint1, $sellingpoint2, $ +sellingpoint3, $summarySELLING, $longSELLING, $skuGROUPimage, $skuGRO +UPaltIMAGE); my $file =<"SyncItemMaster.xml">; my $filename = <"Hashstructure.txt">; my $twig=XML::Twig->new( twig_handlers=> { 'oa:Codes[@name="Small_Package_Indicator"]'=> \&smallpack +, 'oa:Note[@status="Selling_Point_1"]'=> \&sell1, 'oa:Note[@status="Selling_Point_2"]'=> \&sell2, 'oa:Note[@status="Selling_Point_3"]'=> \&sell3, 'oa:Note[@status="Summary_Selling_Statement"]'=> \&summary +, 'oa:Note[@status="Long_Selling_Copy"]'=> \&longsell, 'us:SkuGroupImage' => \&image, 'us:SkuGroupAlternateImage' => \&altimage, 'oa:Code[@name="SKU_Group_Id"]'=> \&skuid, } ); $twig->parsefile("500SyncItemMaster.xml"); our %mirror; sub skuid{ my ($t, $node)=@_; my $prefix=$node->parent->parent->parent->first_child->nex +t_sibling->text; my $stock=$node->parent->parent->parent->first_child->next +_sibling->next_sibling->text; my $itemnumber="$prefix$stock"; my $value=$node->text; my $column="skuGROUPnumber"; $mirror{"$itemnumber"}={ $column =>"$value"}; #...lots of similar subs... open(my $fh,'>:utf8', "$filename") or die "Could not open $filename:$!"; my @k = (sort keys %mirror); foreach my $k (@k){ my $v = $mirror{$k}; my %hash = %$v; foreach my $h (keys %hash){ print $fh ("$k => \n $h => $hash{$h}\n")}; # print + the hash }
I've used this exact syntax to print to a file before, but this time when I used it I am receiving the following error. Could not open "Hashstructure.txt":Invalid argument at line 141. I'm not really sure what the conflict is. I've read the "Inavlid argument" threads and none of them really seem to apply. Google also fails me. Any help appreciated.

Replies are listed 'Best First'.
Re: filename flagged as Invalid argument
by Jenda (Abbot) on Dec 28, 2007 at 14:59 UTC

    $node->parent->parent->parent->first_child->next_sibling? Looks frail to me. What if the order gets different? What if the next version of the XML inserts a new tag somewhere? Most likely there is a better way to write this. Show us the XML.

    Another thing. There is no point in writing "$variable" where $variable suffices. There are very very few cases when the doublequotes around variable names are needed.

      I'd like to, but the XML is massive. To get a concept of the structure I'd have to include about 200 lines of XML. The file itself is over 22 million lines. Thanks for the help though!
Re: filename flagged as Invalid argument
by almut (Canon) on Dec 28, 2007 at 14:22 UTC
    my $filename = <"Hashstructure.txt">;

    Not entirely sure if it has to do with your problem, but why are you using globbing to declare the file name?  As a consequence of the glob, the file name will include the double quotes. I would rather try a simple

    my $filename = "Hashstructure.txt";

    Which OS are you using? Maybe it doesn't accept double quotes as part of a file name...

Re: filename flagged as Invalid argument
by FunkyMonk (Bishop) on Dec 28, 2007 at 14:27 UTC
    my $filename = <"Hashstructure.txt">; open my $F, ">", $filename or die $filename, $!;

    Works here (Linux, Perl 5.10.0) but looks very unusual. What do you expect to accomplish using the glob?

Re: filename flagged as Invalid argument
by jonnyfolk (Vicar) on Dec 28, 2007 at 14:26 UTC
    I would be suspicious of your definition of $file and $filename.
    my $file = 'SyncItemMaster.xml'; my $filename = 'Hashstructure.txt';
    is more what I would expect to see, though I am unfamiliar with what you are trying to do...