develooper Front page | perl.perl5.porters | Postings from September 2021

Re: Pre-RFC: command-line flag for slurping

Thread Previous | Thread Next
From:
Oodler 577 via perl5-porters
Date:
September 29, 2021 18:55
Subject:
Re: Pre-RFC: command-line flag for slurping
Message ID:
YVS2iatc45n5pYmC@odin.sdf-eu.org
* Scott Baker <scott@perturb.org> [2021-09-29 08:37:57 -0700]:

> On 9/29/21 08:21, Dan Book wrote:
> > On Wed, Sep 29, 2021 at 11:00 AM Tomasz Konojacki <me@xenu.pl> wrote:
> > 
> >     "-0777" flag is the usual way to read the whole file at once
> >     (instead of
> >     line by line) in one-liners.
> > 
> >     I feel this isn't ideal. "-0" is a bad flag. It's overly general,
> >     users
> >     rarely need $/ to be set to anything other than undef or "\n".
> >     Also, the
> >     input record separator has to be specified as an octal number,
> >     which is
> >     weird. The fact that the numbers above 0o377 are special-cased to mean
> >     "undef" makes it even more confusing.
> > 
> >     Slurping is an extremely common operation and it deserves its own
> >     one-letter flag. I propse "-g" (mnemonics: gobble, grab, gulp). I wish
> >     it could be "-s", but sadly it's already taken :(
> > 
> > 
> > I think this is an excellent idea. This sort of processing using Perl
> > oneliners is extremely common and spread across the internet, and the
> > '-0777' flag is a constant source of confusion. Ideally perl would have
> > support for long options so we didn't have to take up the dwindling
> > one-letter options, but in the meantime, they are not in high demand so
> > IMO it's fine to use one for this.

Seems like anything would need to consider also -n and -l, any others? I'm not
a perl oneliner wizard by any means, but these two came up in my google.

#Note:
#$ perl -v
#This is perl 5, version 32, subversion 1 (v5.32.1) built for x86_64-netbsd-thread-multi

...

-0777, only

  $ perl -0777 -MO=Deparse -e 'chomp; print $_' ./file.txt 
  BEGIN { $/ = undef; $\ = undef; }
  chomp $_;
  print $_;
  -e syntax OK
  
-l, only

  $ perl -l -MO=Deparse -e 'chomp; print $_' ./file.txt 
  BEGIN { $/ = "\n"; $\ = "\n"; }
  chomp $_;
  print $_;
  -e syntax OK
  
-n, only 

  $ perl -n -MO=Deparse -e 'chomp; print $_' ./file.txt 
  LINE: while (defined($_ = readline ARGV)) {
      chomp $_;
      print $_;
  }
  -e syntax OK
  
-l -0777,

  $ perl -l -0777 -MO=Deparse -e 'chomp; print $_' ./file.txt 
  BEGIN { $/ = undef; $\ = "\n"; }
  chomp $_;
  print $_;
  -e syntax OK

-n -0777,
  
  $ perl -n -0777 -MO=Deparse -e 'chomp; print $_' ./file.txt 
  BEGIN { $/ = undef; $\ = undef; }
  LINE: while (defined($_ = readline ARGV)) {
      chomp $_;
      print $_;
  }
  -e syntax OK

-l -n -0777, (note "double chomp" - not quite sure where that came from)
  
  $ perl -l -n -0777 -MO=Deparse -e 'chomp; print $_' ./file.txt 
  BEGIN { $/ = undef; $\ = "\n"; }
  LINE: while (defined($_ = readline ARGV)) {
      chomp $_;
      chomp $_;
      print $_;
  }
  -e syntax OK

Cheers,
Brett

ps: I like the idea of "long" options, too.

> > 
> > -Dan
> 
> I totally support this feature... and I think it should be a --long option
> instead of "-g". The Perl interpretter is *long overdue* for long options.
> 
> - Scott

-- 
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About