简体   繁体   中英

Perl regex extracting a match using braces

I tested the following code

#! /usr/bin/perl 

use strict;
use English;

#this code extracts the current scripts filename 
#by removing the path from the filepath

my $Script_Name = $PROGRAM_NAME;

${Script_Name} =~ s/^.*\\//; #windows path

#${Script_Name} =~ s/^.*\///; #Unix based path

print $Script_Name;

and i don't understand why these braces extract the match without using a /r modifier. can anyone explain why and how this works or point me to some documentation?

You're getting a little confused!

The braces make no difference. ${Script_Name} is identical to $Script_Name .

You code first copies the entire path to the script file from $PROGRAM_NAME to $Script_Name .

Then the substitution removes everything up to and including the last backslash, leaving just the file name.

The /r modifier would be used if you wanted to modify one string and put the result of the modification into another, so you could write your code in one step as

$Script_Name = $PROGRAM_NAME=~ s/^.*\\//r

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM