-2

My code

use strict;

use warnings;

my $filename = '263.xml';

open(my $fh, $filename)

perl -p -i -e '$fh if /<caldata chopper="on"/ "(\d+)"/; print $fh;' 

But

Warning: Use of "-p" without parentheses is ambiguous at a1.pl line 7.
syntax error at a1.pl line 7, near ")

perl "
Execution of a1.pl aborted due to compilation errors.

I want to separate line with

caldata chopper="on"

and caldata chopper="off" into separate files. This is the whole line

<caldata chopper="on" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
  • What you posted as "_My code_" makes no sense. Do you literally have a line with `perl ...` in a script? The `perl` an unquoted bareword in a script. The regex itself is fine (no need to escape `"` or anything else). However, how `$fh` is used is wrong -- it's a filehandle. Also, you are clearly missing a `;` on that `line 7` from (the first) error. – zdim Aug 26 '16 at 17:53
  • @zdim Yes,I am beginner with perl.How should the line look like? – Richard Rublev Aug 26 '16 at 17:55
  • OK. It is far from valid Perl. `open` line needs a `;`. The `$fh` is a _filehandle_, which gives access to the resource (file) -- you use it to read data from said resource, not to print it. So you'd need a loop like `while(<$fh>)`. There is no keyword `perl` in the language. The statement `$fh if /.../` makes no sense, I don't see what you mean by it. The firts part of the regex, `/.../`, is fine, but the rest (second `/.../`) does not make sense. Etc. I'd suggest to go through the basics of the language first. – zdim Aug 26 '16 at 18:02
  • When I use "_no sense_" I mean to say that it doesn't conform to the most basic language rules and that I can't even see what you want to do with it. I don't mean to be offensive. – zdim Aug 26 '16 at 18:11

1 Answers1

6

To clarify why the posted code "doesn't make sense", perl -p -i -e is something you would type on the command line by itself, not something which goes into a Perl program. Basically, the perl -p -i -e '...' is itself the program to run. perldoc perlrun explains in greater detail, but:

  • -p puts a loop around the ... code which runs it against each line of the input file
  • -i means to edit the input file in place (instead of creating any new files for output)
  • -e tells perl that you're providing executable code as part of the command line, rather than running a program from a file

The correct way to do what you were attempting would be something like (warning - untested code):

#!/usr/bin/env perl

use strict;
use warnings;

open my $in, '<', '263.xml' or die "Can't open input file: $!";
open my $out_on, '>', 'on.xml' or die "Can't open 'on' output file: $!";
open my $out_off, '>', 'off.xml' or die "Can't open 'off' output file: $!";

while (my $line = <$in>) {
  $line =~ /<caldata chopper="(on|off)"/;
  if ($1 eq 'on') {
    print $out_on, $line;
  } elsif ($1 eq 'off') {
    print $out_off, $line;
  }
}

Note, however, that this technique will not create proper XML output files. It will just generate files containing lists of caldata elements without any of the additional surrounding elements needed to have a single, well-formed XML document. If you need a well-formed XML document, I would suggest taking a look at XML::Twig rather than attempting to parse or generate XML by hand.

Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102