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

Hi,

I need to create five files . So I store five values in array.Those value are my file handler and file name

use strict; my $start='<?xml version="1.0" encoding="utf-8"?> <MultifamilyProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta +nce" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'; my @key=("1","2","3","4","5"); my ($file_handle,$output_file); $output_file=".xml"; for(my $i=0;$i<$#key;$i++) { my $val=$key[$i]; $file_handle=$val; $output_file=$val.$output_file; open($file_handle,'>:utf8',"$output_file"); print $file_handle $start."\n"; print $file_handle "\t\t".'<Address>'."\n"; close $file_handle; }

If i comment Use Strict line .My program is running and five files is created but Without commenting Use Strict i got error like

Can't use string ("1") as a symbol ref while "strict refs" in use at D +:\senthil\ xmlgeneration\xmltest\file.pl line 12.

Regards,

Senthil

Replies are listed 'Best First'.
Re: Error coming while using Use Strict
by Corion (Patriarch) on Oct 13, 2011 at 09:32 UTC

    The error happens in the following line. Please explain what the line is supposed to do:

    print $file_handle $start."\n";

    Please also explain what this line is supposed to do:

    $file_handle=$val;

    Maybe you want to read and understand open? None of the examples shown match your usage.

    The relevant part for your error is

    If FILEHANDLE is an undefined scalar variable (or array or hash element), a new filehandle is autovivified, meaning that the variable is assigned a reference to a newly allocated anonymous filehandle. Otherwise if FILEHANDLE is an expression, its value is the real filehandle. (This is considered a symbolic reference, so use strict "refs" should not be in effect.)

    My advice is to just not do that and to use the documented examples instead.

      hi,

      use strict; my $start='<?xml version="1.0" encoding="utf-8"?> <MultifamilyProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta +nce" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'; my @key=("1","2","3","4","5"); my ($file_handle,$output_file); $output_file=".xml"; for(my $i=0;$i<$#key;$i++) { my $val=$key[$i]; $output_file=$val.$output_file; open($val,'>:utf8',"$output_file"); print $val $start."\n"; print $val "\t\t".'<Address>'."\n"; close $val; }

      You may understand this code

      print $val $start."\n"; which is used to assigning $start variable content to my each file handler

      Regards,

      Senthil

        You don't need a different filehandle when you access files sequentially. Also the previous contents of the filehandle variable is quite meaningless to its use as a filehandle. So just use something like this:

        for(my $i=0;$i<$#key;$i++) { my $val; #No need to put anything in it $output_file=$key[$i].$output_file; open($val,'>:utf8',"$output_file");

        How is this post supposed to answer the two questions I asked?

Re: Error coming while using Use Strict
by JavaFan (Canon) on Oct 13, 2011 at 10:54 UTC
    The problem is that the first argument of open should either be a filehandle, or a reference to a filehandle. Since 5.6, open will autovivify an undefined value into a filehandle (most people say "since 5.6, we have lexical filehandles". That's not true. Neither is the first argument of open a filehandle in this case, nor is 5.6 a requirement to have a lexical reference to a filehandle. Both have been possible long before 5.6 - it's only the autovivification that's new). But you're passing in a variable with a defined value; that's fine, Perl will try to use it as a reference to a filehandle. And the *1 typeglob does have a slot for filehandles. It will all work fine and dandy, except that by enabling strict 'refs', you're explicitely forbidden Perl to do its utter best to satisfy your request.

    If you'd just replace $filehandle=$val; with my $filehandle;, it should all work.

Re: Error coming while using Use Strict
by Marshall (Canon) on Oct 13, 2011 at 12:14 UTC
    Your program logic appears to me to be giberish.

    Back up and write in words what you are trying to do.
    "My program is running", no I do not think that is true at all.

    Start with writing a couple of sentences about what you are trying to do.

Re: Error coming while using Use Strict
by priyaviswam (Sexton) on Oct 13, 2011 at 10:12 UTC
    Hi Senthil,

    you don't need different handle Please find the updated code

    use strict; my $start='<?xml version="1.0" encoding="utf-8"?> <MultifamilyProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta +nce" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'; my @key=("1","2","3","4","5"); my ($file_handle,$output_file); for(my $i=0;$i<$#key;$i++) { my $val=$key[$i]; print "val:$val\n"; my $output_file=$val.".xml"; print "out : $output_file\n"; open(OPEN,'>:utf8',"$output_file"); print OPEN $start."\n"; print OPEN "\t\t".'<Address>'."\n"; close OPEN; }

Re: Error coming while using Use Strict
by jwkrahn (Abbot) on Oct 13, 2011 at 11:16 UTC

    What you want is:

    use warnings; use strict; my $start = '<?xml version="1.0" encoding="utf-8"?> <MultifamilyProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta +nce" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'; my @keys = 1 .. 5; my $output_file = ".xml"; for my $val ( @keys ) { $output_file = $val . $output_file; open my $file_handle, '>:utf8', $output_file or die "Cannot open ' +$output_file' because: $!"; print $file_handle "$start\n\t\t<Address>\n"; }
      I doubt the files with longer names are desired:
      54321.xml 4321.xml 321.xml 21.xml