Welcome to Netrider ... Connecting Riders!

Interested in talking motorbikes with a terrific community of riders?
Signup (it's quick and free) to join the discussions and access the full suite of tools and information that Netrider has to offer.

Coding in Perl - Anyone here know much about it?

Discussion in 'The Pub' at netrider.net.au started by Bosco, Jun 26, 2008.

  1. Anyone here know how to code in perl? I'm stuck with a problem and pulling my hair out trying to resolve it... - I'm new to perl btw.

  2. I haven't coded from scratch, but I've done a lot of editing and modifying other people's Perl (usually CGI scripts) to make them do what I want them to do. Perhaps if we combine our ignorance?
  3. What I'm after is the following:

    Using the Win32:StopService module I want to be able to stop windows services which i've listed in an array. I'm able to stop them via their actual service name but not by the number associated with it in the array.


    100 UPnP Device Host
    101 User Profile Service
    108 Windows Audio
    109 Windows Audio Endpoint Builder
    113 Windows Connect Now - Config Reg
    114 Windows Defender
    115 Windows Driver Foundation - User
    116 Windows Error Reporting Service
    118 Windows Event Log

    Here is the code that i'm using for that bit which is a sub routine:

    sub started
    foreach $key(@sorted)
    Win32::Service::GetStatus("", $service{$key}, \%status);
    if ($status{CurrentState} == 4)
    print "\t$counter\t$key\n" ;

    print "Please enter the number of the service to terminate:\n";

    chomp($user_selection = <STDIN>);
    Win32::Service::GetServices ("", \%services);
    foreach (sort(keys(%services)))
    $service_name = $_;
    print "$_";
    system ("pause");
    my %service;
    Win32::Service::GetStatus ("", $service{$key}, \%status);
    $running = $status{"CurrentState"};
    if (("$service_name" eq "$user_selection") and
    ("$running" eq "4"))
    print "$service_name $codes{$running} and will be stopped\n";
    Win32::Service::StopService ("", $service_name);
    print "$service_name has been stopped! \n";

    I'm clueless as to what I'm doing wrong here.... so any help would be greatly appreciated.
    Also if you need any more info just lemme know...

    Thx in advance
  4. Really more guess than science, but I wondered whether in this section:

    if (("$service_name" eq "$user_selection") and
    ("$running" eq "4"))

    you could use '==' in place of 'eq'

    I also wondered if the 'and' logical statement here is correctly formed

    That seems to be the likely breakdown point, IMO, but I don't know the syntax well enough to know what's wrong (if anything).

  5. Sorry, the '==' is for only the second one, which is a number value comparison: 'eq' is for a string comparison, which the first one is.

    Oh, I guess the other thing is that the entered number might be getting treated as a number and being compared to a string? Is it worth recoding and using A, B, C, etc to see if it helps?

    As said, thrashing around and exploring rather than speaking from any basis of knowledge: I think phizog is a real coder...
  6. Well at the moment you have if service_name eq user input
    do stuff

    shouldn't you change your service_name in this case to the service id number?
  7. Bah I hate debugging.

    Don't have any experience with perl, infact perl syntax makes my eyes bleed :grin:

    My only suggestion is to debug each module individually, too hard to design it all and then debug the whole thing - take one module and test that it works, eg. outputting the result to a text message so you know what its doing. Do this for each module and you'll likely solve the problem.

    Also try a perl forum, the other likely thing is you've got a syntax error there somewhere, thats the most annoying type of error IMO because you can spent so long trying to fix it thinking its a logic error, only to realise you used () instead of [] somewhere :oops:
  8. They say that only 5% of IT graduates end up programming. I hate to disappoint so stayed with the majority. :bolt:
  9. thx for the help all.... I shall keep having a go at it...

    But in all seriousness........ I cant code for shit hahahahaha