• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

urgent-understanding this line in perl script

hi
can anyone tell me what this means in perl

/\n.*//s

from here
elsif ($sel eq "exception")
{
   $xpath = '/*/Exception';
   $sub = '/\n.*//s';
}
basically this is extracting Exception node from teh XML file. but what is $sub doing?

can someone suggest? also is there any documentation where i can get this info.
thanks a lot
0
samir25
Asked:
samir25
  • 6
  • 3
  • 3
  • +3
3 Solutions
 
TintinCommented:
The documentation is in

perldoc perlre

The relevant part being:

     s   Treat string as single line.  That is, change "." to
         match any character whatsoever, even a newline, which
         normally it would not match.

0
 
samir25Author Commented:
sorry i donot have perl on my machine. i just need to know what is the criteria for this parsing of the node from the xml file.

i still didnt understand what it means by this line.
$sub = '/\n.*//s';

actually this node is not completely extracted but a part of it. so i want to what part to extract (i have to do similar thing in my pl/sql)
0
 
Adam314Commented:
The part in single quotes is a regular expression pattern.
If you don't have perl on your machine, you can get the doc taht Tintin is referring to at:
http://perldoc.perl.org/perlre.html

The expression means:
\n             Match a newline
.*             Match any characters (normally exclude newline), as many times as possible
/s             Allow . to match a newline.  Otherwise . will match anything except newline
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
DropZoneCommented:
Adam314's answer is the proper one, but just to make sure you understand, the line means "remove anything after the end of line".  In essence, the code you posted sets a regular expression that (pressumably) will be used later on to clean up the text of the exception to only contain the first line.

    -dZ.
0
 
ozoCommented:
the line
 $sub = '/\n.*//s';
assigns the literal string containing / \ n , * / / s
to the variable $sub
I don't what you then do with the contents of $sub or whether it ever gets interpreted as a regular expression.  That would depend on the rest of your program.
0
 
mjcoyneCommented:
I agree with ozo -- I stayed out of the thread because thought I was missing something, but if you run:

#!/usr/bin/perl -w
$sub = '/\n.*//s';
print $sub;

it prints:

/\n.*//s

My first problem was the lack of a beginning 's' (as in s/\n.*//s) to cause the substitution to occur.  So, while it *looks* a lot like a regular expression, it isn't acting as one in the expression you've shown (which is just an assignment of a string to the scalar variable $sub).

If the variable $sub is evaluated as a regular expression later on (a possibility ozo mentioned), I'm not sure it would do what (it seems) it's intended to do, because of the missing substitution operator...
0
 
DropZoneCommented:
In my comment I mention that "pressumably" will be used as a regular expression substitution later on.  It is not, indeed, a command to replace, but a string representing a regular expression substitiution.  You asked what the code did, and we tried to infer by that piece what would happen.  But without seeing the entire code, this is impossible.  Those lines by themselves do not do anything more than sets strings, but since it iis silly to assume that those few lines comprise the entire program, I was under the impression that you were trying to find out what the overall use is.
0
 
samir25Author Commented:
hi
is anyone of you there..
can i upload the script? its not very big but wouldnt like to paste it here as it will be little lengthy
0
 
Adam314Commented:
you can post it at www.ee-stuff.com.  Use your same username/password as experts-exchange.com
0
 
samir25Author Commented:
ok i will just do it.
thanks a lot
0
 
samir25Author Commented:
done
0
 
ozoCommented:
If somewhere else in the script there is an expression like
eval "s$sub"
that would tend to support the interpretation that it was meant to be part of a s/\n.*//s operator
or it may have been part of
eval "y$sub"
or
print $sub

0
 
samir25Author Commented:
i ve uploaded the script. would you be able to help in knowing...what part of the Exception node is extracted. this node may have around 10 lines but only 2-3 imp lines are pulled out. i want to know what is the criteria. what does reg exp mean here. in asp.net it would i would use for error checking email message. what all characters would be allowed to enter in the email box
0
 
samir25Author Commented:
did you get to read the txt file?
0
 
ozoCommented:
     foreach my $re ($sub, @sub)
      {
          eval "\$text =~ s$re";
      }
means that if $sub is '/\n.*//s'
then we execute $text =~ s/\n.*//s
where s/\n.*//s is interpreted as explained above

but you may want to be careful about eval'ing a string that can be set by the user,
or a malicious user may be able to trick you into executing arbitrary code
0
 
DropZoneCommented:
QUOTE: "what part of the Exception node is extracted. this node may have around 10 lines but only 2-3 imp lines are pulled out. i want to know what is the criteria. what does reg exp mean here.'

The script attempts to parse an XML file and prepare the text of nodes for output.  In the case of an "exception" node, as I mentioned above, only the first line is included, and all other lines after the first are discarded.  The criteria is the first "\n" (newline) character found.  According to the script ONLY the first line should be included.  If you see that the first 2 or 3 lines are included, then it could be because those first few lines do not really contain what the operating system considers a new line.  This could be because they are "DOS" newlines (Carriage-return + Line-feed) on a UNIX environment, or UNIX newlines (Line-fee) on a Windows environment.

As ozo mentioned the code below performs this.  I have added my own comments to make it clear what is going on.

# Find known nodes in the file and prepare a
# regular expression to extract their text.
if ($sel eq "rxbias")
{
   # handle this node
}
...

# Found an "exception" node, lets extract its text
elsif ($sel eq "exception")
{
   # Set the xpath to this node so that we may find it later
   $xpath = '/*/Exception';

   # Set a regular expression to extract only the first line
   # The regular expression means the following:
   # find the first newline (\n) and everything else after it,
   # (including other newlines), and replace them with an
   # empty string.  The "/s" at the end means treat the
   # string as a single line, which means that the dot
   # will match not only any character, but also newlines.
   $sub = '/\n.*//s';
}

   # Loop through al the nodes of the typed requested
   # by the user (in the -sel command line option).
   foreach my $node (in $nodes)
   {
      # Get the entire text for this node
      my $text = $node->text;

      # Loop through all the substitution regexp patters
      # set in the above if/elsif code and evaluate it as
      # a regular expression substitution.
      foreach my $re ($sub, @sub)
      {
          # Construct a regular expression substitution
          # command string and evaluate it as code.
        eval "\$text =~ s$re";
      }

      # Add the cleaned-up text to an array
      # to be displayed later on.
      push(@fields, $text);
   }

As ozo said, you should be very careful in evaluating strings nilly-willy, as it could be dangerous.  Although in this case the evaluated string is set by the program and not the user, it does include text provided by the user in the XML file.  So a maliciously crafted string in one of the known nodes could cause it to do harm.

    I hope this helps.
    -dZ.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 6
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now