Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Regular expressions

by user786 (Sexton)
on Apr 10, 2015 at 17:58 UTC ( #1123055=perlquestion: print w/replies, xml ) Need Help??

user786 has asked for the wisdom of the Perl Monks concerning the following question:

I have written a function which is in a library, and I call the functions from my test script.

The function returns 0.

ideally it should return Starting strongSwan 5.1.2 IPsec

I checked the regular expression in . My regex seems to be correct. /(Starting strongSwan.*IPsec$)/ .

Can somebody help me out with the return value of the function.

Function :

sub ipsec_restart { my ($self) = @_; my $cmd = 'sudo -s ipsec restart'; my $restart = 0; $self->execute($cmd); foreach my $line ( @{ $self->get_stdout() } ) { if ( $line =~ /(Starting strongSwan.*IPsec$)/ ) { $restart = $1; last; } } return $restart; }

Function call :

$self->{'ipsec_restart'} = $self->{'ipsec_obj'}->ipsec_restart(); ('[Startup] ipsec restart status is : ' . $self->{'ipsec_res +tart'} );

Expected output : See the highlighted text below.

 Starting strongSwan 5.1.2 IPsec  Actual output is  : 0

Replies are listed 'Best First'.
Re: Regular expressions
by RichardK (Parson) on Apr 10, 2015 at 18:11 UTC

    Are you 100% sure that $self->get_stdout() is returning what you expect it to return? (Did you forget to chomp?)

    Try adding print '<',$line,">\n"; to the start of the foreach loop.

    Also see Basic Debugging Checklist for lots of other suggestions on how to debug any problem.

Re: Regular expressions
by GotToBTru (Prior) on Apr 10, 2015 at 18:10 UTC

    Do you know what the contents of $line is when you apply your regex?

    Dum Spiro Spero

      Stopping strongSwan IPsec...

      Starting strongSwan 5.1.2 IPsec starter...

        The $ at the end of your regex says it should match up to the end of the string. If anything (including a space) is appearing after "IPsec", then the string doesn't end with "IPsec" and the match will fail. Remove the $, and it should work.

        Dum Spiro Spero
Re: Regular expressions
by NetWallah (Canon) on Apr 10, 2015 at 18:19 UTC
    You are making the assumption that all the output you see when you run "ipsec restart" from a console comes from a single process.

    My (very limited) understanding of strongSwan is that it uses a "starter" process which forks out a thread to do the actual work.

    I think you are capturing the output of the "starter" process, and missing the actual ipsec messages.

    You can confirm my theory by examining the console messages after running your program - if you see the "Starting strongSwan" on the cosole, it means that your code did not capture it.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Regular expressions
by ww (Archbishop) on Apr 10, 2015 at 20:06 UTC

    Just in case this is 'new' info for you, the ".*" (fondly known as 'dot splat') in

    $line =~ /(Starting strongSwan.*IPsec$)/

    will match ZERO or more instances of ANY char except a newline. The asterisk is a quantifier (NOT A WILDCARD; that's the dot). For more, see or type (at your console) perldoc perlre (or if you need to perldoc perlretut) and similar.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1123055]
Front-paged by GotToBTru
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-01-30 04:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found