Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have some comments on the script itself, even though that's not what you were posting about.

I already mentioned use strict; use warnings. But also, what version of Perl are you using? If it is reasonably up to date, you can use the new "given/when" construct.

You are not declaring your variables. This will be caught by strict, and mis-spelled variables is one of the easiest mistakes to make and find automatically.

You are using explicit variable name in constructs that use $_ implicitly, but still using $_ for the variable. If you are going to use a name, use a named local variable. Or if you use $_, leave it off. For serious programming (as opposed to quick one liners or one-time-use scripts) you use the explicit form as you did, but use a name.

foreach my $line (@configFile) { next if $line =~ /^\s*$/; ...
You don't need the parens around the condition in the suffix (statement modifier) form. I don't see where @configFile is coming from. It will be empty! So there might be stuff you're not showing in the example.

Now the cool up-to-date stuff. If you use Perl 5.10 or higher, you can use given/when. But, that works with a foreach as well, so I'll revert to using the implicit $_ form:

foreach (@configFile) { chomp; # careful of last line in file next if /^\s*$/; # empty line next if /^\s*#/; # comment line when (/^\s*ScriptName:\s+\S+\s*$/) { ($tag,$scriptName) = split(); } when (/^\s*LogFileName:\s+\S+\s*$/) { ($tag,$logFileName) = split(); } when (/^\s*NumberOfSteps:\s+[0-9]+\s*$/) { ($tag,$numberOfSteps) = split(); } when (/\s*Restart:\s+\S+\s*$/) { ($tag,$restart) = split(); } when (/^\s*Step\s+[0-9]:/) { $stepNum = $_; $stepNum =~ s/^\s*Step\s+([0-9]+):.*$/$1/; $command = $_; $command =~ s/^\s*Step [0-9]:+\s+//; $step[$stepNum] = $command; } default { print STDERR "Error: unknown config file line:\n"; print STDERR " $_\n"; $error = 1; } }
Now the use of split splits on whitespace, but you already went through the string with the regexp and found the space. So use the results of the regexp instead of splitting again:
when (/^\s*(ScriptName:)\s+(\S+)\s*$/) { $tag= $1; $scriptName= $2; }
Note that you are requiring a space after the colon and before the name. If that is an artefact of your use of the simple split, you don't need that this way. Change the + to a * if you like.

Note finally that [0-9] is common enough to have an optimized abbreviation: \d for digit.

The stuff for Step is rather convoluted. In the same mold that I just showed, try:

when (/^\s*Step\s+(\d):\s*(.*)/) { $stepNum = $1; $command = $2; $step[$stepNum] = $command; }
But there is really no reason to note the $tag in these lines, as it was just a byproduct of your use of split. This limits you to 10 steps (0 through 9) as single digits. If you used \d+ it would be open ended.

But your following loop starts at 1, not zero. So even though you accept a step 0, you never use it.

There's really no need to specify the "number of steps". Just use whatever is in the array when you've read it:

foreach my $command (@step) { next unless defined $command; # skip unused numbers my $ret= system ($command); ... }

In reply to Re: How to "transfer control" to child process? by John M. Dlugosz
in thread How to "transfer control" to child process? by VingInMedina

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2021-04-13 21:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?