Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this line in DBI.pm do?

Tags:

regex

perl

603   $dsn =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i
                         or '' =~ /()/; # ensure $1 etc are empty if match fails

I don't understand what $dsn =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i is for,even more doubt about '' =~ /()/,seems useless to me..

like image 574
new_perl Avatar asked Feb 23 '26 21:02

new_perl


1 Answers

The first part is extracting two parts of the dsn string in the form:

dbi: first match ( optional second match ) :

These matches will be placed into $1 and $2 for the use in later code. The second part will only run if the match was unsuccessful. This is achieved by using or which will short-circuit (i.e. not execute) the second expression if the first one was successful.

As the comment says quite succinctly, it ensures that $1, $2, etc. are empty. Presumably so later code can check them and produce an appropriate error if they were not set (i.e. could not be extracted from the dsn string).

like image 90
a'r Avatar answered Feb 25 '26 13:02

a'r