user1340802
user1340802

Reputation: 1157

understanding a simple Perl construct

as a not Perl programmer, i would like to be sure i had well understood a construct that i am going to port to Python,

when using :

if (s/^([$PChar])(.)/$2/) {
  print $1,"\n";
  $finished = 0;
}

what i am really not sure is does the matching/replacement is done before the print $1 ? and is it done "inplace" inside current buffer (which is $F, that is $_ line by line readed, splitted on its space character), that is changing it (so if i understand well, the ([$PChar]) when @ beginning of a string is totally striped off/lost in the above statment) ?

EDIT : no maybe it is not lost, first parenthesis part is captured, and then printed as $1 + new line character and then... no, do not understand what become $2... may be buffer change to second parenthesis part ? /END OF EDIT.

also is there any environnement or what is the best environnement that permit to do some step-by-step debugging on Win platform ? i'm aware that having this, i will not have asked this question. And i do not need to learn Perl, just only to be able to read and adapt this script.

here is the englobing part :

@F = split;
for( $j=0; $j<=$#F; $j++) {
  my $suffix="";
  $_ = $F[$j];
  # separate punctuation and parentheses from words
  do {
$finished = 1;
# cut off preceding punctuation
if (s/^([$PChar])(.)/$2/) {
  print $1,"\n";
  $finished = 0;
}
# cut off trailing punctuation
if (s/(.)([$FChar])$/$1/) {
  $suffix = "$2\n$suffix";
  $finished = 0;
}

whole script tokenize.pl can be seen here while original tar.bz if from here

best regards

Upvotes: 0

Views: 165

Answers (1)

codaddict
codaddict

Reputation: 454922

# try to delete the first character from the string contained in
# $_ if that character is one of the characters contained in
# the string $PChar. The deletion is done by replace the first and
# second character by only the second character.
if (s/^([$PChar])(.)/$2/) {

  # if the replacement was successful, print the deleted character.
  print $1,"\n";
  $finished = 0;
}

Upvotes: 2

Related Questions