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

I have script comments (adding % latex file) following some lines with %, the problem is that I want to comment on the lines just before the first \begin{document} and not found after this. I use split and tie:: file, but the script to comment all lines (\usepa...) in the file. I want is to read and edit before \ begin {document} and not after the specified line, then save the changes to that file, any help is appreciated... this is the code
#!/usr/bin/perl -w use strict; use Tie::File; #open my $archivo = 'test-pdf.tex'; my @coment=split(/\\begin{document}/, $archivo,2); #Usamos tie tie @coment, 'Tie::File', $archivo or die "No coment lines: $!"; #search foreach my $line (@coment) { # Coment $line =~ s/(\\usepackage){pst/\%\\usepackage{pst/i; } #close untie @coment;
example test-pdf.tex (before run script)
\RequirePackage{graphicx} \let\IncludeGraphics\includegraphics \graphicspath{{images/}} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage{pstricks} \usepackage{pstricks-add} \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdftex \begin{document} Example code \begin{verbatim} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage{pstricks} \usepackage{pstricks-add} \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdflatex \begin{document} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(8,2){Hola} \end{pspicture} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(4,2){foo bar} \end{pspicture} \end{document} \end{verbatim} Obtain \IncludeGraphics[scale=1]{test-tmp-0.pdf} and \IncludeGraphics[scale=1]{test-tmp-1.pdf} \end{document}
this is output test-pdf.tex (after run script)
\RequirePackage{graphicx} \let\IncludeGraphics\includegraphics \graphicspath{{images/}} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} %\usepackage{pstricks} this is ok %\usepackage{pstricks-add} this is ok \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdftex \begin{document} Example code \begin{verbatim} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} %\usepackage{pstricks} %%%%this is wrong %\usepackage{pstricks-add} %%%%this is wrong \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdflatex \begin{document} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(8,2){Hola} \end{pspicture} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(4,2){foo bar} \end{pspicture} \end{document} \end{verbatim} Obtain \IncludeGraphics[scale=1]{test-tmp-0.pdf} and \IncludeGraphics[scale=1]{test-tmp-1.pdf} \end{document}
Thank for comments, (i think the problem its in open or split file)...

Replies are listed 'Best First'.
Re: split and tie::file
by davido (Cardinal) on May 30, 2011 at 04:44 UTC

    After this line:

    my $archivo = 'test-pdf.tex';

    $archivo contains the exact literal text, "test-pdf.tex". That means the literal text held in $archivo doesn't have "\begin{document}" anywhere in it, so there's nothing for split to work with.

    If you printed the contents of $archivo, or the contents of @coment, you would see that the primary failure is right there. Also, it's unnecessary to chomp the lines. The default behavior for Tie::File is to auto-chomp, and then replace the record separator (usually '\n') behind the scenes as the line is written back to the file. If you want to completely remove '\n', you'll need to set Tie::File to override the default behavior. You probably don't want or need to do this, but if you do, it's handled by the autochomp => 0 option upon initiating the tie.

    As for opening the key file, you actually need to use open, as in, "open my $fh, '<', $archivo or die $!;, and then read first line of the file as my $line = <$fh>; (for example).


    Dave

      many thank, but, woks (no perfect) but works,tried your coments, and post
        Try to make the comments you give me, but I do not understand the examples given in perldoc5, look elsewhere and so may help you open the file (correctly), and separate it into $preamble $document, modify $preamble (comment lines) to save the changes made in the $preamble (join?? to $document) and save the same file. Thank you very much for your time and help with this newbie in perl.
Re: split and tie::file
by Khen1950fx (Canon) on May 30, 2011 at 05:53 UTC
    You need to use the verbatim package, something such as this:
    #!/usr/bin/perl use strict; use warnings; use Tie::File; __END__ -- test -- [% USE Latex; out = FILTER latex(format="pdf"); -%] \usepackage{verbatim} \begin{comment} This is a comment Ditto Double-Ditto \end{comment} \documentationclass{article} \begin{document} \section{{Introduction} \end{document} [% END; -%] [% out | head(4) %] -- expect -- %PDF
Re: split and tie::file
by Anonymous Monk on May 30, 2011 at 02:44 UTC
    #open my $archivo = 'test-pdf.tex';
    No, this does not open test-pdf.tex, see perlintro
      but, found in test-pdf.tex this is test-pdf.tex (in)
      \RequirePackage{graphicx} \let\IncludeGraphics\includegraphics \graphicspath{{images/}} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage{pstricks} \usepackage{pstricks-add} \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdftex \begin{document} Example code \begin{verbatim} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage{pstricks} \usepackage{pstricks-add} \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdflatex \begin{document} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(8,2){Hola} \end{pspicture} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(4,2){foo bar} \end{pspicture} \end{document} \end{verbatim} Obtain \IncludeGraphics[scale=1]{test-tmp-0.pdf} and \IncludeGraphics[scale=1]{test-tmp-1.pdf} \end{document}
      this is output test-pdf.tex (after run script)
      \RequirePackage{graphicx} \let\IncludeGraphics\includegraphics \graphicspath{{images/}} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} %\usepackage{pstricks} this is ok %\usepackage{pstricks-add} this is ok \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdftex \begin{document} Example code \begin{verbatim} \documentclass[10pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern} %\usepackage{pstricks} %%%%this is wrong %\usepackage{pstricks-add} %%%%this is wrong \usepackage[spanish]{babel} \usepackage{microtype}% sólo pdflatex \begin{document} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(8,2){Hola} \end{pspicture} \begin{pspicture}[showgrid=true](4,2) \psscaleboxto(4,2){foo bar} \end{pspicture} \end{document} \end{verbatim} Obtain \IncludeGraphics[scale=1]{test-tmp-0.pdf} and \IncludeGraphics[scale=1]{test-tmp-1.pdf} \end{document}
      thanks, give me a example pliss