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

Hi monks,

I've recently started into perl again and find that a lot of what I "knew" is no longer valid. It would seem that File::Slurp is outmoded. If I'm going to replace it with Path::Tiny, maybe I should do likewise with Path::Class, as suggested at this link by Path::Tiny's author. path class versus path tiny

I'm using Path::Tiny for a pet project just to get my feet wet, and I think I'm using it correctly, but I'm not sure why I'm being warned about wide characters. If I use utf8; and $path1->lines_utf8;, am I not projecting my intent? Where I'm really stumbling is with the logic of how to format this cleanly.

What I want is for the one-word titles to be centered. There are 71 characters in my lines. Vertical whitespace is to be preserved, that is, blank lines remain blank lines. Then I want the newlines removed if there is text on the next line. Such a look-ahead regex is well beyond my abilities, so I am asking for help. What I want in the end is for paragraphs to look like paragraphs with a 5-space indentation. I'm also doing some word substitution to make these promises more ecumenical for the less-believing types, who will bristle at the G-word. Here is the script:

#!/usr/bin/perl -w use strict; use 5.010; use Path::Tiny; use utf8; # creating Path::Tiny objects my $dir = path("/home/bob"); my $name = "1.promises.txt"; my $new_name = path($dir,"1.revised.txt"); my $path1 = path($dir, $name); # list say $path1; my $path2 = path("."); say $path2; # cwd my $path3 = path("~user/file.txt"); # tilde processing say $path3; #say "lines are @lines"; my @lines = $path1->lines_utf8; foreach my $i (0 .. $#lines) { say "i is $i"; say "$lines[$i]"; $lines[$i] =~ s/God/an arbitrary higher power/; $lines[$i]=~ s/Бог/боль&#10 +96;ая сила/; } $new_name->spew_utf8( @lines ); __END__

I am getting partial results in that I have output with the substitutions. Here is the source text, similar to many such texts I might pull out of the pdf's in which they originate:

                      Oбещания   

  Если эта фаза нашего развития болезненна для нас, мы
будем удивлены, когда половина пути окажется позади. Мы
познáем новую свободу и новое счастье. Мы не будем сожалеть
о нашем прошлом и вместе с тем не захотим полностью
забывать о нем. Мы узнаем, что такое чистота, ясность, покой.
Как бы низко мы ни пали в прошлом, мы поймем, что наш опыт
может быть полезен другим. Исчезнут ощущения ненужности и
жалости к себе. Мы потеряем интерес к вещам, которые
подогревают наше самолюбие, и в нас усилится интерес к
другим людям. Мы освободимся от эгоизма. Изменится наше
мировоззрение, исчезнут страх перед людьми и неуверенность
в экономическом благополучии. Мы интуитивно будем знать, как
вести себя в ситуациях, которые раньше нас озадачивали. Мы
поймем, что Бог делает для нас то, что мы не смогли сами
сделать для себя.

     Не слишком ли это звучит многообещающе? Нет. Все это
произошло со многими из нас, с одними раньше, с другими
позже. Все это становится явью, если приложить усилия.

                      Promises

       If we are painstaking about this phase of our development, we will be amazed
     before half through! We are going to know a new freedom and happiness. We
     will not regret the past nor wish to shut the door on it. We will comprehend the
     word serenity and know peace. No matter how far down the scale we have
     gone, we will see how our experience can benefit others. That feeling of use-
     lessness and self-pity will disappear. We will lose interest in selfish things and
     gain interest in our fellows. Self-seeking will slip away. Our whole attitude
     and outlook upon life will change. Fear of people and of economic insecurity
     will leave us. We will intuitively know how to handle situations which used to
     baffle us. We will suddenly realize that God is doing for us what we could not
     do for ourselves.

       Are these extravagant promises? We think not. They are being fulfilled among
     us—sometimes quickly, sometimes slowly. They will always materialize if we
     work for them.

Typical output

$ ./1.tiny.pl 
/home/bob/1.promises.txt
.
~user/file.txt
i is 0
Wide character in say at ./1.tiny.pl line 26.
                       Oбещания   

i is 1


i is 2
Wide character in say at ./1.tiny.pl line 26.
  Если эта фаза нашего развития болезненна для нас, мы

i is 3
Wide character in say at ./1.tiny.pl line 26.
будем удивлены, когда половина пути окажется позади. Мы

It would seem that I only have bad choices for displaying this material as the p tags destroy the whitespace and the c tags make the cyrillic unreadable. Thank you for your comment.

Update: attempting to use pre tags.

Replies are listed 'Best First'.
Re: using Path::Tiny and reformating result
by haukex (Chancellor) on Jun 26, 2018 at 19:09 UTC
    I'm not sure why I'm being warned about wide characters. If I use utf8; and $path1->lines_utf8;, am I not projecting my intent?

    Not fully: The utf8 pragma tells Perl that your source file is encoded in UTF-8 (nothing else), and ->lines_utf8 reads the file as a UTF-8 encoded file. The thing missing here is the output: your STDOUT is not set up to output Unicode. Try adding the open pragma: use open qw/:std :utf8/;

    As for your formatting questions, I'm not entirely sure I understand - is your output fixed-width, like in a terminal or <pre> tags, or is it HTML?

      Thx haukex, that got rid of the warning. I'm not sure that I understand it completely, but I do understand the absence of a warning. I do have suitable output to frame the problem. It's 39 lines of text all told, so if seeing that much data is going to make the reader grumpy, then don't open the readmore tags. I'm hoping to get the pre tags right on this. (Order seems to matter on these tags.)

      New script:

      #!/usr/bin/perl -w
      use strict;
      use 5.010;
      use Path::Tiny;
      use utf8;
      use open qw/:std :utf8/;
       
      # creating Path::Tiny objects
       
      my $dir = path("/home/bob");
      my $name = "1.promises.txt";
      my $new_name = path($dir,"1.revised.txt");
       
      my $path1 = path($dir, $name); # list
      say $path1; 
      #say "lines are @lines";
      my @lines = $path1->lines_utf8;
      
      foreach my $i (0 .. $#lines) 
      {
       say "i is $i";
       say "$lines$i";
       $lines$i =~ s/God/an arbitrary higher power/;
       $lines$i=~ s/Бог/большая сила/;
      }
      
      $new_name->spew_utf8( @lines );
      
      __END__ 
      
      

      A couple problems solved, but I'm still fishing for how I format this into homogenous-looking paragraphs.

      Update: attempting again to use pre tags successfully; using them in conjunction with code tags seems not to display wide characters properly

        attempting again to use pre tags successfully; using them in conjunction with code tags seems not to display wide characters properly

        Yes, the issue with <code> vs. <pre> tags and Unicode characters only working in the latter is a known issue on PerlMonks.

        I'm still fishing for how I format this into homogenous-looking paragraphs.

        Have a look at the core module Text::Wrap. Here's one way, I'm using the "paragraph mode" supported by $/ to read the file:

        use warnings; use strict; use open qw/:std :utf8/; use Text::Wrap 'wrap'; open my $fh, '<:raw:encoding(UTF-8)', '1.promises.txt' or die $!; local $/ = ''; local $Text::Wrap::columns = 71; while (<$fh>) { s/^\s+|\s+$//mg; if (/\A\w+\z/) { print ' ' x int( (71-length)/2 ), $_, "\n"; next; } s/\n|\s{2,}/ /g; print wrap(' 'x5, '', $_), "\n"; } close $fh;

        Output:

                                       Oбещания
             Если эта фаза нашего развития болезненна для нас, мы будем
        удивлены, когда половина пути окажется позади. Мы познáем новую
        свободу и новое счастье. Мы не будем сожалеть о нашем прошлом и вместе
        с тем не захотим полностью забывать о нем. Мы узнаем, что такое
        чистота, ясность, покой. Как бы низко мы ни пали в прошлом, мы поймем,
        что наш опыт может быть полезен другим. Исчезнут ощущения ненужности и
        жалости к себе. Мы потеряем интерес к вещам, которые подогревают наше
        самолюбие, и в нас усилится интерес к другим людям. Мы освободимся от
        эгоизма. Изменится наше мировоззрение, исчезнут страх перед людьми и
        неуверенность в экономическом благополучии. Мы интуитивно будем знать,
        как вести себя в ситуациях, которые раньше нас озадачивали. Мы поймем,
        что Бог делает для нас то, что мы не смогли сами сделать для себя.
             Не слишком ли это звучит многообещающе? Нет. Все это произошло со
        многими из нас, с одними раньше, с другими позже. Все это становится
        явью, если приложить усилия.
                                       Promises
             If we are painstaking about this phase of our development, we
        will be amazed before half through! We are going to know a new freedom
        and happiness. We will not regret the past nor wish to shut the door
        on it. We will comprehend the word serenity and know peace. No matter
        how far down the scale we have gone, we will see how our experience
        can benefit others. That feeling of use- lessness and self-pity will
        disappear. We will lose interest in selfish things and gain interest
        in our fellows. Self-seeking will slip away. Our whole attitude and
        outlook upon life will change. Fear of people and of economic
        insecurity will leave us. We will intuitively know how to handle
        situations which used to baffle us. We will suddenly realize that God
        is doing for us what we could not do for ourselves.
             Are these extravagant promises? We think not. They are being
        fulfilled among us—sometimes quickly, sometimes slowly. They will
        always materialize if we work for them.
        
Re: using Path::Tiny and reformating result
by Anonymous Monk on Jun 26, 2018 at 23:12 UTC