Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 11, 2023 02:39 pm GMT

4 More Unrelated Perl Tidbits

Last year I wrote an article titled 4 Unrelated Perl Tidbits, where I talked about some random Perl facts I learned about from reading Programming Perl. In this article I will talk about 4 more random and interesting Perl features I have learned about since.

Built-Ins Can Be Overridden with Lexical Subroutines

Perl version 5.18 introduced lexical subroutines, which are often sometimes referred to as "my subs". An interesting characteristic of lexical subs is that unlike regular subroutines, they can override built-ins.

use v5.18;my sub print {    die "printing is banned
";}print "Hello, World!
";__END__$ perl tmp.plprinting is banned

If anybody has seen a legitimate use of this feature then please comment below.

Recursive Anonymous Subroutines With SUB

Perl version 5.16 introduced the __SUB__ special token that holds a reference to the current subroutine. You can use __SUB__ to make a recursive call in an anonymous subroutine.

use v5.16;higher_order_subroutine(    sub {        my $n = shift;        if ($n <= 1) { return 1 }        else         { return $n * __SUB__->($n-1) }    })

Goto Searches For Labels In The Dynamic Scope

The following example shows that goto searches for its label argument from within the current dynamic scope. Note that this program just goes on forever printing hello from after LABEL:

sub foo {    bar();}sub bar {    goto LABEL;}sub baz {  LABEL:    print "hello from after LABEL
"; foo();}baz();__END__$ perl tmp.plhello from after LABELhello from after LABELhello from after LABEL...

This is another feature that you should leave a comment about if you have seen a legitimate usage of it.

Regex Modifier For Only Portions Of The Regex

You can use the (?M:) pattern in a regex to turn on the modifier specified by M, only inside the parentheses. For example, the following two regexs are the same:

/foo/i/(?i:foo)/

You can also turn a modifier off with (?-M:), which is shown in this example:

if ('FOO' =~ /(?-i:foo)/i) {    print "matches
"} else { print "does not match
"}__END__$ perl tmp.pldoes not match

This feature is useful if you want to turn a modifier on/off for only a portion of the regex:

if ('fooBAR' =~ /(?-i:foo)bar/i) {    print "matches
"} else { print "does not match
"}__END__$ perl tmp.plmatches

Original Link: https://dev.to/nicholasbhubbard/4-more-unrelated-perl-tidbits-ijm

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To