in reply to How do I modify a line within the file itself?

You should always use strict. It will save your life. Start now.

You should always open files with three arguments.

You should make your code as readable as possible, including use of $_ as the default input.

use strict; use warnings; my $file = $ARGV[0]; open(my $fh, "<", $file) or die "Can't open $file: $!"; while ( <$fh> ) { chomp; # $_ is default input if ( /\>/ ) { # $_ is still default input, # match is default operation my @modify_line = split( /\s/ ); # $_ is still default input my $line = $modify_line[0]; # if you are going to use an array print $line; }

Not that you should do it this way. Use File::Slurp::Tiny.

Remember: Ne dederis in spiritu molere illegitimi!

Replies are listed 'Best First'.
Re^2: How do I modify a line within the file itself?
by Anonymous Monk on Jul 08, 2015 at 23:53 UTC

    Not that you should do it this way. Use File::Slurp::Tiny.

    I wouldn't recommend anything starting with "File::Slurp"

    Path::Tiny is what I'd recommend

      I wouldn't recommend anything starting with "File::Slurp"

      That's an incredibly ignorant thing to say. File::Slurp is over 20 years old and is a dependency in more than 500 CPAN distributions. Do a little research on Uri Guttman.

      Leon Timmermans, one of the prominent Perl contributors of the new generation, author of File::Slurp::Tiny, is not exactly known for unreliable code, either.

      To the OP: some of what you will receive here in response to your questions is utter crap. Do your own due diligence and evaluate the advice you get in other parts of the Perl realm ...

      Remember: Ne dederis in spiritu molere illegitimi!

        I wouldn't recommend anything starting with "File::Slurp" That's an incredibly ignorant thing to say. File::Slurp is over 20 years old and is a dependency in more than 500 CPAN distributions. Do a little research on Uri Guttman. Leon Timmermans, one of the prominent Perl contributors of the new generation, author of File::Slurp::Tiny, is not exactly known for unreliable code, either. To the OP: some of what you will receive here in response to your questions is utter crap. Do your own due diligence and evaluate the advice you get in other parts of the Perl realm ...

        Hmm, I don't think its ignorant. You recommended one thing, I did another.

        That File::Slurp is old, that the authors are prolific and respected , that is all great, but doesn't negate my experience

        I prefer Path::Tiny because of the api, its very memorable, the File::Slurp... api not so much

        I much prefer slurp_raw to  read_file( 'filename', { binmode => ':raw' } ) ;

        Its rare to only want to slurp, and Path::Tiny also does all the things that go along with , all that File::Spec/File::Copy/File::Path stuff

        File::Slurp is over 20 years old

        ... and for much of that time had serious issues surrounding Unicode handling, and at the moment still has 26 open bugs, its last release was over four years ago... sorry, but there are real reasons why the usual recommendation I've heard is against File::Slurp, in addition to the fact that slurping a file is something that can be done in three lines of Perl code.

        some of what you will receive here in response to your questions is utter crap

        As far as I can tell the anon you responded to has been posting here for much longer than you, and knows what they're talking about. Don't be too quick to judge.