CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need help in decoding the following PERL code

 



showale
New User

Mar 11, 2013, 2:53 PM

Post #1 of 2 (338 views)
Need help in decoding the following PERL code Can't Post

can any one help decoding following....I have it in my source code



Mainly I would like to know ,what is there within this if condition if (! /\|$/



and is it like , while <not eof > read one line from file, remove newline from that end of the line and then print that line to testdata.emp.tmp ?



perl -e 'while (<>) { if (! /\|$/ ) { chomp; } print ;}' $testdata.emp> $testdata.emp.tmp



thanks


Kenosis
User

Mar 11, 2013, 3:21 PM

Post #2 of 2 (334 views)
Re: [showale] Need help in decoding the following PERL code [In reply to] Can't Post

This perl script's executed from the command line, reading each line of $testdata.emp and removes the end-of-record (a newline, in this case) from each line if the line doesn't end with "|". Output is directed to $testdata.emp.tmp

Here's the regex:

Code
!/\|$/  
^^^^^^
||||||
|||||+ - End match
||||+ - End of line
|||+ - Verticle bar's escaped (so it's not regarded as alternation)
||+ - Escape
|+ - Begin match
+ - Not the case


So, the condition evaluates to true and the line is chomped if the line doesn't end with "|".

In a script with __DATA__ :

Code
use strict; 
use warnings;

while (<DATA>) {
if ( !/\|$/ ) {
chomp;
}
print;
}

__DATA__
Line 1
Line 2|
Line 3
Line 4|
Line 5
Line 6|


Output:

Code
Line 1Line 2| 
Line 3Line 4|
Line 5Line 6|


Lines 1, 3 & 5 don't end with "|", so their ending newline was removed.


(This post was edited by Kenosis on Mar 11, 2013, 3:22 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives