Is there a less tedious way to use Perl chomp?










2















Very frequently I have to do:



#similarly in while (my $val = <$fh>)...
my $val = <$fh>;
chomp $val;

my $res = `command`;
chomp $res;


I'd rather skip the second line if I could. I see I can use the -l option in my shebang line based on: Is there anything in core Perl to auto-chomp lines from "<>" operator?



Is there something similar for backticks? Or alternately, is there a way to chomp inline that is less verbose?










share|improve this question

















  • 5





    it is not a bad thing to have code that does what it says.

    – ysth
    Nov 14 '18 at 3:55











  • @ysth of course, and I wouldn't accept a solution that isn't that way. I wasn't too fond of the "-l" option put in the shebang line because it's easy to miss. I don't like using two lines, and I don't want to do my $x = <>; chomp $x; in one line.

    – Automaton
    Nov 14 '18 at 19:32















2















Very frequently I have to do:



#similarly in while (my $val = <$fh>)...
my $val = <$fh>;
chomp $val;

my $res = `command`;
chomp $res;


I'd rather skip the second line if I could. I see I can use the -l option in my shebang line based on: Is there anything in core Perl to auto-chomp lines from "<>" operator?



Is there something similar for backticks? Or alternately, is there a way to chomp inline that is less verbose?










share|improve this question

















  • 5





    it is not a bad thing to have code that does what it says.

    – ysth
    Nov 14 '18 at 3:55











  • @ysth of course, and I wouldn't accept a solution that isn't that way. I wasn't too fond of the "-l" option put in the shebang line because it's easy to miss. I don't like using two lines, and I don't want to do my $x = <>; chomp $x; in one line.

    – Automaton
    Nov 14 '18 at 19:32













2












2








2








Very frequently I have to do:



#similarly in while (my $val = <$fh>)...
my $val = <$fh>;
chomp $val;

my $res = `command`;
chomp $res;


I'd rather skip the second line if I could. I see I can use the -l option in my shebang line based on: Is there anything in core Perl to auto-chomp lines from "<>" operator?



Is there something similar for backticks? Or alternately, is there a way to chomp inline that is less verbose?










share|improve this question














Very frequently I have to do:



#similarly in while (my $val = <$fh>)...
my $val = <$fh>;
chomp $val;

my $res = `command`;
chomp $res;


I'd rather skip the second line if I could. I see I can use the -l option in my shebang line based on: Is there anything in core Perl to auto-chomp lines from "<>" operator?



Is there something similar for backticks? Or alternately, is there a way to chomp inline that is less verbose?







perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:16









AutomatonAutomaton

1237




1237







  • 5





    it is not a bad thing to have code that does what it says.

    – ysth
    Nov 14 '18 at 3:55











  • @ysth of course, and I wouldn't accept a solution that isn't that way. I wasn't too fond of the "-l" option put in the shebang line because it's easy to miss. I don't like using two lines, and I don't want to do my $x = <>; chomp $x; in one line.

    – Automaton
    Nov 14 '18 at 19:32












  • 5





    it is not a bad thing to have code that does what it says.

    – ysth
    Nov 14 '18 at 3:55











  • @ysth of course, and I wouldn't accept a solution that isn't that way. I wasn't too fond of the "-l" option put in the shebang line because it's easy to miss. I don't like using two lines, and I don't want to do my $x = <>; chomp $x; in one line.

    – Automaton
    Nov 14 '18 at 19:32







5




5





it is not a bad thing to have code that does what it says.

– ysth
Nov 14 '18 at 3:55





it is not a bad thing to have code that does what it says.

– ysth
Nov 14 '18 at 3:55













@ysth of course, and I wouldn't accept a solution that isn't that way. I wasn't too fond of the "-l" option put in the shebang line because it's easy to miss. I don't like using two lines, and I don't want to do my $x = <>; chomp $x; in one line.

– Automaton
Nov 14 '18 at 19:32





@ysth of course, and I wouldn't accept a solution that isn't that way. I wasn't too fond of the "-l" option put in the shebang line because it's easy to miss. I don't like using two lines, and I don't want to do my $x = <>; chomp $x; in one line.

– Automaton
Nov 14 '18 at 19:32












3 Answers
3






active

oldest

votes


















4














sub chomper(_) 
my ($line) = @_;
chomp($line) if defined($line);
return $line;


while (defined( my $line = chomper(<>) ))
...






share|improve this answer

























  • Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

    – Automaton
    Nov 14 '18 at 19:01











  • defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

    – ikegami
    Nov 14 '18 at 19:03












  • I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

    – Automaton
    Nov 14 '18 at 19:14












  • I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

    – ikegami
    Nov 14 '18 at 19:17












  • About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

    – Automaton
    Nov 15 '18 at 0:32



















3














Another option to reduce tedium is to learn to love $_



while(<$fh>) 
chomp;


local $_ = <$fh>;
chomp;

local $_ = `command`;
chomp;


Same number of lines, but now they are half as long :)



edit: Corrected thanks to @ysth's comment, learnt something new today






share|improve this answer




















  • 1





    <$fh>; throws away a line; it only implicitly sets $_ in a while expression

    – ysth
    Nov 14 '18 at 3:53






  • 1





    While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

    – Grinnz
    Nov 14 '18 at 23:49


















3














You can wrap chomp around the whole expression



chomp(my $date = `date`);
say $date;


For other suggestions on a sort of "auto-chomp" on filehandles, see this answer.



Update: There's also a Backtick::AutoChomp module, which is implemented with a source filter.



EDIT



I originally also had the following snippet without actually testing it



while (chomp(my $line = <$fh>)) 
say $line;



As per ikegami's comment, this is unreliable and will misbehave in various ways.






share|improve this answer




















  • 2





    Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

    – ikegami
    Nov 14 '18 at 2:59











  • Also -1 for suggesting a source filter as a solution to any problem

    – mob
    Nov 14 '18 at 3:53











  • I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

    – Joshua
    Nov 14 '18 at 8:11






  • 1





    I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

    – Automaton
    Nov 14 '18 at 19:05











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291813%2fis-there-a-less-tedious-way-to-use-perl-chomp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














sub chomper(_) 
my ($line) = @_;
chomp($line) if defined($line);
return $line;


while (defined( my $line = chomper(<>) ))
...






share|improve this answer

























  • Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

    – Automaton
    Nov 14 '18 at 19:01











  • defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

    – ikegami
    Nov 14 '18 at 19:03












  • I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

    – Automaton
    Nov 14 '18 at 19:14












  • I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

    – ikegami
    Nov 14 '18 at 19:17












  • About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

    – Automaton
    Nov 15 '18 at 0:32
















4














sub chomper(_) 
my ($line) = @_;
chomp($line) if defined($line);
return $line;


while (defined( my $line = chomper(<>) ))
...






share|improve this answer

























  • Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

    – Automaton
    Nov 14 '18 at 19:01











  • defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

    – ikegami
    Nov 14 '18 at 19:03












  • I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

    – Automaton
    Nov 14 '18 at 19:14












  • I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

    – ikegami
    Nov 14 '18 at 19:17












  • About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

    – Automaton
    Nov 15 '18 at 0:32














4












4








4







sub chomper(_) 
my ($line) = @_;
chomp($line) if defined($line);
return $line;


while (defined( my $line = chomper(<>) ))
...






share|improve this answer















sub chomper(_) 
my ($line) = @_;
chomp($line) if defined($line);
return $line;


while (defined( my $line = chomper(<>) ))
...







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 19:17

























answered Nov 14 '18 at 2:57









ikegamiikegami

263k11177397




263k11177397












  • Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

    – Automaton
    Nov 14 '18 at 19:01











  • defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

    – ikegami
    Nov 14 '18 at 19:03












  • I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

    – Automaton
    Nov 14 '18 at 19:14












  • I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

    – ikegami
    Nov 14 '18 at 19:17












  • About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

    – Automaton
    Nov 15 '18 at 0:32


















  • Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

    – Automaton
    Nov 14 '18 at 19:01











  • defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

    – ikegami
    Nov 14 '18 at 19:03












  • I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

    – Automaton
    Nov 14 '18 at 19:14












  • I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

    – ikegami
    Nov 14 '18 at 19:17












  • About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

    – Automaton
    Nov 15 '18 at 0:32

















Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

– Automaton
Nov 14 '18 at 19:01





Thanks, I could see this being useful in a common lib. I understand you need this scalar implementation when calling chomper on a <HANDLE> to not slurp up all lines at once. Otherwise maybe this variant can be used for both scalars and arrays sub chomper chomp(@_) if defined(@_); return @_;

– Automaton
Nov 14 '18 at 19:01













defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

– ikegami
Nov 14 '18 at 19:03






defined(@array) makes no sense. And your changes make it unusable to the OP (without adding scalar around the argument in the caller)

– ikegami
Nov 14 '18 at 19:03














I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

– Automaton
Nov 14 '18 at 19:14






I understand the OP is for scalar context. I'm trying to see if both scalars and arrays can be handled as native chomp can, i.e. something that would allow my @files = chomper(`ls`);

– Automaton
Nov 14 '18 at 19:14














I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

– ikegami
Nov 14 '18 at 19:17






I just said you could... but I take it back. It would make for a really weird sub. Best to make it only work on a scalar, and use map to apply it to each element of a list. (my @files = map chomper, `ls`)

– ikegami
Nov 14 '18 at 19:17














About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

– Automaton
Nov 15 '18 at 0:32






About _ I found the following (learned a new thing): "As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead." I'm still unclear on why we need the if defined condition.

– Automaton
Nov 15 '18 at 0:32














3














Another option to reduce tedium is to learn to love $_



while(<$fh>) 
chomp;


local $_ = <$fh>;
chomp;

local $_ = `command`;
chomp;


Same number of lines, but now they are half as long :)



edit: Corrected thanks to @ysth's comment, learnt something new today






share|improve this answer




















  • 1





    <$fh>; throws away a line; it only implicitly sets $_ in a while expression

    – ysth
    Nov 14 '18 at 3:53






  • 1





    While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

    – Grinnz
    Nov 14 '18 at 23:49















3














Another option to reduce tedium is to learn to love $_



while(<$fh>) 
chomp;


local $_ = <$fh>;
chomp;

local $_ = `command`;
chomp;


Same number of lines, but now they are half as long :)



edit: Corrected thanks to @ysth's comment, learnt something new today






share|improve this answer




















  • 1





    <$fh>; throws away a line; it only implicitly sets $_ in a while expression

    – ysth
    Nov 14 '18 at 3:53






  • 1





    While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

    – Grinnz
    Nov 14 '18 at 23:49













3












3








3







Another option to reduce tedium is to learn to love $_



while(<$fh>) 
chomp;


local $_ = <$fh>;
chomp;

local $_ = `command`;
chomp;


Same number of lines, but now they are half as long :)



edit: Corrected thanks to @ysth's comment, learnt something new today






share|improve this answer















Another option to reduce tedium is to learn to love $_



while(<$fh>) 
chomp;


local $_ = <$fh>;
chomp;

local $_ = `command`;
chomp;


Same number of lines, but now they are half as long :)



edit: Corrected thanks to @ysth's comment, learnt something new today







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 4:12

























answered Nov 14 '18 at 1:41









lodlod

953612




953612







  • 1





    <$fh>; throws away a line; it only implicitly sets $_ in a while expression

    – ysth
    Nov 14 '18 at 3:53






  • 1





    While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

    – Grinnz
    Nov 14 '18 at 23:49












  • 1





    <$fh>; throws away a line; it only implicitly sets $_ in a while expression

    – ysth
    Nov 14 '18 at 3:53






  • 1





    While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

    – Grinnz
    Nov 14 '18 at 23:49







1




1





<$fh>; throws away a line; it only implicitly sets $_ in a while expression

– ysth
Nov 14 '18 at 3:53





<$fh>; throws away a line; it only implicitly sets $_ in a while expression

– ysth
Nov 14 '18 at 3:53




1




1





While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

– Grinnz
Nov 14 '18 at 23:49





While this is nice for short programs, be wary: $_ is a global variable, and code you call can easily clobber it in the middle of your loop; also, while(<$fh>) does not localize its usage of $_, and so will inflict the same on any outer code. Assigning to a lexical variable avoids both these problems.

– Grinnz
Nov 14 '18 at 23:49











3














You can wrap chomp around the whole expression



chomp(my $date = `date`);
say $date;


For other suggestions on a sort of "auto-chomp" on filehandles, see this answer.



Update: There's also a Backtick::AutoChomp module, which is implemented with a source filter.



EDIT



I originally also had the following snippet without actually testing it



while (chomp(my $line = <$fh>)) 
say $line;



As per ikegami's comment, this is unreliable and will misbehave in various ways.






share|improve this answer




















  • 2





    Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

    – ikegami
    Nov 14 '18 at 2:59











  • Also -1 for suggesting a source filter as a solution to any problem

    – mob
    Nov 14 '18 at 3:53











  • I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

    – Joshua
    Nov 14 '18 at 8:11






  • 1





    I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

    – Automaton
    Nov 14 '18 at 19:05
















3














You can wrap chomp around the whole expression



chomp(my $date = `date`);
say $date;


For other suggestions on a sort of "auto-chomp" on filehandles, see this answer.



Update: There's also a Backtick::AutoChomp module, which is implemented with a source filter.



EDIT



I originally also had the following snippet without actually testing it



while (chomp(my $line = <$fh>)) 
say $line;



As per ikegami's comment, this is unreliable and will misbehave in various ways.






share|improve this answer




















  • 2





    Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

    – ikegami
    Nov 14 '18 at 2:59











  • Also -1 for suggesting a source filter as a solution to any problem

    – mob
    Nov 14 '18 at 3:53











  • I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

    – Joshua
    Nov 14 '18 at 8:11






  • 1





    I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

    – Automaton
    Nov 14 '18 at 19:05














3












3








3







You can wrap chomp around the whole expression



chomp(my $date = `date`);
say $date;


For other suggestions on a sort of "auto-chomp" on filehandles, see this answer.



Update: There's also a Backtick::AutoChomp module, which is implemented with a source filter.



EDIT



I originally also had the following snippet without actually testing it



while (chomp(my $line = <$fh>)) 
say $line;



As per ikegami's comment, this is unreliable and will misbehave in various ways.






share|improve this answer















You can wrap chomp around the whole expression



chomp(my $date = `date`);
say $date;


For other suggestions on a sort of "auto-chomp" on filehandles, see this answer.



Update: There's also a Backtick::AutoChomp module, which is implemented with a source filter.



EDIT



I originally also had the following snippet without actually testing it



while (chomp(my $line = <$fh>)) 
say $line;



As per ikegami's comment, this is unreliable and will misbehave in various ways.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 8:14

























answered Nov 14 '18 at 1:32









JoshuaJoshua

1,695812




1,695812







  • 2





    Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

    – ikegami
    Nov 14 '18 at 2:59











  • Also -1 for suggesting a source filter as a solution to any problem

    – mob
    Nov 14 '18 at 3:53











  • I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

    – Joshua
    Nov 14 '18 at 8:11






  • 1





    I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

    – Automaton
    Nov 14 '18 at 19:05













  • 2





    Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

    – ikegami
    Nov 14 '18 at 2:59











  • Also -1 for suggesting a source filter as a solution to any problem

    – mob
    Nov 14 '18 at 3:53











  • I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

    – Joshua
    Nov 14 '18 at 8:11






  • 1





    I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

    – Automaton
    Nov 14 '18 at 19:05








2




2





Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

– ikegami
Nov 14 '18 at 2:59





Bad! This solution throws a warning if the last line ends with a line feed or if the file is empty, and it skips the last line otherwise.

– ikegami
Nov 14 '18 at 2:59













Also -1 for suggesting a source filter as a solution to any problem

– mob
Nov 14 '18 at 3:53





Also -1 for suggesting a source filter as a solution to any problem

– mob
Nov 14 '18 at 3:53













I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

– Joshua
Nov 14 '18 at 8:11





I specifically mentioned it uses a source filter so the reader can make their own decision on whether they're ok with that. If it makes it any better, the source filter is parsed using PPI, which makes it some-what more reliable, but of course not perfect.

– Joshua
Nov 14 '18 at 8:11




1




1





I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

– Automaton
Nov 14 '18 at 19:05






I could use this part: chomp(my $date = `date`); I had tried something close before posting this and it failed with "Can't modify scalar chomp in scalar assignment" - that's because I missed adding the parens.

– Automaton
Nov 14 '18 at 19:05


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291813%2fis-there-a-less-tedious-way-to-use-perl-chomp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3