Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Grab Current Line of SVG File

by beginAgain (Novice)
on Apr 17, 2019 at 14:55 UTC ( #1232717=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have been tasked with "correcting" certain items in SVG files. I have never used Perl and after a crash course (using PerlMonks, Stack Overflow, etc.) I can successfully read the SVG file line by line and correct the necessary lines. My final problem, and this is admittedly because of my inexperience, I cannot figure out how to grab the current line so I can do a smartmatch (or something similar). All I need to do is make sure that the current line starts with "<path" and process that line if so. Below is the (partial) code where I read in the file and loop through the lines to make the corrections. EDIT - Ihave added more of the code to show what I want to do with smartmatch - NOTE thta target is set like so:
my $target = '<path';
opendir IN, $dirname; my @in = grep { /^[^.]/ } readdir IN; closedir IN; for my $in (@in) { open IN, '<', "$dirname/$in" || next; open OUT, '>', "$outdirname/$in" || die "can't open file output/$in" +; foreach(<IN>) { #read file line by line # Strings to correct Note: Escaped ( with \ s/rotate\(-180/rotate\(-0/g; # Look at the current line my @look_in = <IN>; # <path is in this line, process line if( $target ~~ @look_in) { for my $key (keys %stroke_width_hash) { s/$key/$stroke_width_hash{$key}/g; } } # Print out line to OUT file print OUT $_; } close OUT; close IN; }
I have tried (sad attempts):
Print (<IN>)
Print IN
etc.
But none of these are grabbing the actual text for that line
Any help will be greatly appreciated...
Charles

Replies are listed 'Best First'.
Re: Grab Current Line of SVG File
by SuicideJunkie (Vicar) on Apr 17, 2019 at 15:14 UTC

    What you would probably want to do is read line by line with:

    while (my $line = <IN>) { #Do stuff with $line $line =~ s/rotate\(-180/rotate\(-0/g; #etc }
    (The minimalist for loop you have reads the whole file in at once, then loops over the lines with the current line stored in $_. Your subsequent code implicitly uses $_ with the substitutions and whatnot, which is what is tripping you up I believe.)

    You also have an issue with your opens; "$dirname/$in" || next is unlikely to do the next, since "$dirname/$in" isn't going to be false. You want to use "or" instead of "||" there for the precedence.

    You may also want to upgrade to open my $inFileHandle, '<', "$dirname/$in" or next;, which will make it easy to pass $inFileHandle into functions in the future, but that's not important for what you're currently doing.

      You said "with the current line stored in $_" - that saved me - I was able to get the current code to do what I needed with that VERY helpful tidbit and using the index function to check for <path Thank you, Charles
      Thank you for the quick response :) I am working with your suggestions now and will get back to you
Re: Grab Current Line of SVG File
by tangent (Vicar) on Apr 17, 2019 at 15:58 UTC
    You could try something like this:
    opendir( my $dir, $dirname ); my @in = grep { /^[^.]/ && -f "$dirname/$_" } readdir( $dir ); closedir $dir; for my $file (@in) { open( my $infile, '<', "$dirname/$file" ) or next; open( my $outfile, '>', "$outdirname/$file" ) or die "can't open f +ile output/$file: $!"; while ( my $line = <$infile> ) { $line =~ s/rotate\(-180/rotate\(-0/g; if ( $line =~ m/$target/ ) { for my $key (keys %stroke_width_hash) { $line =~ s/$key/$stroke_width_hash{$key}/g; } } print $outfile $line; } close $outfile; close $infile; }

      Thank you for replying :) - I have solved it with something mentioned by another poster - see my comments
Re: Grab Current Line of SVG File
by haukex (Bishop) on Apr 17, 2019 at 21:23 UTC

    Could you show a short but representative input file and the output you expect for that input?

    In general, I would very strongly recommend you don't parse XML with regexes, it's extremely brittle. I recommend a proper XML parser such as XML::LibXML, it will make you much happier in the long run.

Re: Grab Current Line of SVG File
by Anonymous Monk on Apr 18, 2019 at 08:12 UTC
Re: Grab Current Line of SVG File
by dinesh_1308 (Initiate) on Apr 20, 2019 at 17:26 UTC
    $_ will help you to grab current line.. Ex:- while (<$filehandler>){ print $_; # this prints line by line if ($_ =~ / # your search pattern goes here/){ } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1232717]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2022-06-30 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?