I tend to go for ultra portability..
I've already said it doesn't help portability.
On unix, the end of line is \n
On Windows, the end of line is \n
On old Macs, the end of line is \n
On new Macs, the end of line is \n
qr/(?:\r\n|\n\r|\n|\r)/s will likely handle any end-of-line scenario in the modern world.
I didn't deny that. I said it should be centralized. Compare
my $re_single = qr/
(?:\A|[\r\n]) # either the start of text or newline
(
[^\r\n]* # any non-newlines
(?:\z|(?:\r\n|\n\r|\r|\n)\z) # last newline or eof
)
/xs;
my $re_multiple = qr/
(?:\A|[\r\n]) # either the start of text or newline
(
(?:
[^\r\n]*
(?:\r\n|\n\r|\r|\n)
){0,$nless}
) # have to enclose above so it all goes into $1
(
[^\r\n]*
(?:\z|(?:\r\n|\n\r|\r|\n)\z) # last nl or eof
)
/xs;
to
my $re_single = qr/
^ ( .* \n? \z )
/mx;
my $re_multiple = qr/
^
( (?: .* \n ){0,$nless} )
( .* \n? \z )
/mx;
$String =~ s/\r\n|\n\r|\r|\n/\n/g;
In short, it's not Last_N_Lines's job to decode IO.
By the way, there are more improvements you can make.
- $re_single is just a special case of $re_multiple. You can use $re_multiple even when only one line is needed.
- You don't need two captures. Combine them into one.
- This is the perfect place for the ternary operator.
Compare
my $re_single = qr/
(?:\A|[\r\n]) # either the start of text or newline
(
[^\r\n]* # any non-newlines
(?:\z|(?:\r\n|\n\r|\r|\n)\z) # last newline or eof
)
/xs;
sub Last_N_Lines {
my ( $str, $n ) = @_;
my $nless = ( $n - 1 );
my $re_multiple = qr/
(?:\A|[\r\n]) # either the start of text or newline
(
(?:
[^\r\n]*
(?:\r\n|\n\r|\r|\n)
){0,$nless}
) # have to enclose above so it all goes into $1
(
[^\r\n]*
(?:\z|(?:\r\n|\n\r|\r|\n)\z) # last nl or eof
)
/xs;
if ( $n > 1 ) {
if ( $str =~ m/$re_multiple/ ) {
return( "$1$2" );
} else {
return( "" );
}
}
if ( $str =~ m/$re_single/ ) {
return( $1 );
} else {
return( "" );
}
}
to
sub Last_N_Lines {
my ( $str, $n ) = @_;
my $nless = ( $n - 1 );
return $str =~ /
^
(
(?: .* \n ){0,$nless}
.* \n?
)
\z
/mx ? $1 : "";
}
$String =~ s/\r\n|\n\r|\r|\n/\n/g;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.