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

I have not used Perl in several years and decided it would be the best choice for my project; I am not getting any output or warnings any assistance would be greatly appreciated.

hosts.csv data: R17N6340-C04-1;10.1.70.1 R17N6340-C04-2,10.1.70.2 R17N6340-C04-3,10.1.70.3 #!/usr/bin/perl #use strict; #use warnings; my $xml_file = 'template.txt'; my $list_file = 'hosts.csv'; my $out_file = 'newhost.xml'; # open( XML, '<:utf8', $xml_file ); open( LIST, '<:utf8', $list_file ); open( OUT, '>>:utf8', $out_file ); my $xml; { local $/ = undef; $xml = <XML>; }; my $line; foreach $line ( <LIST> ) { # Skipping if the line is empty or a comment next if $line =~ /^\s*$/ ; next if $line =~ /^\s*#/ ; my ($hostname, $interface) = split(/,/, $lin); $xml =~ s/hostname/$hostname/g; $xml =~ s/interface/$interface/g; } print XML $xml; close

Replies are listed 'Best First'.
Re: no output
by toolic (Bishop) on Apr 06, 2016 at 16:48 UTC

    Tip # from the Basic debugging checklist: use strict. $lin is not the same as $line. Other observations:

    • Check success of open
    • Do you really mean to print to your XML input file?
Re: no output
by Eily (Monsignor) on Apr 06, 2016 at 16:53 UTC

    Maybe you'd get warnings if you didn't disable them:

    use strict; use warnings;
    You try to print to XML but have opened it for reading. I suppose you meant OUT. You should add an argument to close, and maybe close all three handles.

Re: no output
by jellisii2 (Hermit) on Apr 06, 2016 at 21:54 UTC
    Please don't parse XML by hand. This is a path to madness. Use one of the fine XML interpreters. I highly recommend XML::Twig.
Re: no output
by stevieb (Canon) on Apr 06, 2016 at 17:04 UTC

    I'm curious about something.

    First, you slurp in the XML document:

    { local $/ = undef; $xml = <XML>; };

    then later, you do a search/replace globally on the entire stringified file:

    $xml =~ s/hostname/$hostname/g; $xml =~ s/interface/$interface/g;

    What that does on first pass, is replace *all* instances of 'hostname' and 'interface' with the first instance of $hostname and $interface. Without seeing the XML template, it's hard to guess if this is what you want.

    Why don't you explain what you're trying to achieve here? It's really not all that clear from what you've provided... are you trying to print out a new file *per host*? That would make the above make sense.

      First thanks for the quick responses; I have a csv file with hosts in on column and their IP address in the second. CSV format:

      FTC-R17N6340-C04-1;10.1.70.1 FTC-R17N6340-C04-2,10.1.70.2 FTC-R17N6340-C04-3,10.1.70.3
      My XML template has two instances of hostname that need to be replaced with the first collumn and one instance of ILO being replaced by the IP address. XML:
      <host> <host>hostname</host> <name>hostname</name> <description/> <proxy/> <status>0</status> <ipmi_authtype>-1</ipmi_authtype> <ipmi_privilege>2</ipmi_privilege> <ipmi_username/> <ipmi_password/> <tls_connect>1</tls_connect> <tls_accept>1</tls_accept> <tls_issuer/> <tls_subject/> <tls_psk_identity/> <tls_psk/> <templates/> <groups> <group> <name>C7000 Chassis</name> </group> </groups> <interfaces> <interface> <default>1</default> <type>1</type> <useip>1</useip> <ip>ILO</ip> <dns/> <port>10050</port>
      After the text is replaced I want the template with the new host information appended to the XML file but leaving the template intact ready for the second line.

        So you are using a template file to render an output file. Use HTML::Template. Despite the name it's a general purpose templating system that does well and consistently what you are trying to do.

        Premature optimization is the root of all job security
Re: no output
by Anonymous Monk on Apr 06, 2016 at 18:04 UTC
    Add use autodie; at the top
Re: no output
by marto (Cardinal) on Apr 06, 2016 at 17:27 UTC

    Looks like you're printing to the wrong filehandle. XML is opened read only yet you try to print to it. You've disabled warnings, don't to this. The valid points above shouldn't be ignored.

    Update: Strikeout, this has already been covered.