in reply to Re^2: Regex match on implicit default variable ($_) in a script, not a one liner
in thread Regex match on implicit default variable ($_) in a script, not a one liner
You aren't aware of what the switches on your one-liner are doing for you. See perlrun
The -n is the most important one. It applies a while(<>) { ... } loop around your code. So, it is taking your input string, splitting it into lines, assigning each value in turn to $_, and then executing the body of your one-liner for each value. In a program, you have to specify that.
#!/usr/bin/perl use strict; use warnings; use diagnostics; my $A="abc\ndef\nghi\n"; my @list = split /\n/,$A; while (<@list>) { ! /def/ and print }
Updated: my most literal attempt didn't work, putting the split inside the while condition. So I use split to create an array. That works better.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Regex match on implicit default variable ($_) in a script, not a one liner
by Todd Chester (Scribe) on Oct 24, 2015 at 20:15 UTC | |
by shmem (Chancellor) on Oct 24, 2015 at 21:42 UTC | |
by AppleFritter (Vicar) on Oct 24, 2015 at 20:30 UTC | |
by Todd Chester (Scribe) on Oct 24, 2015 at 23:07 UTC | |
by AppleFritter (Vicar) on Oct 25, 2015 at 00:09 UTC |