> Hello I have a version of cron I've written in perl
> and am intersted in insuring cross-platofrm functionality.
> I use Geopt::std to get many options, but there does not seem
> to be any way to provide command-line options to perl
> scripts with MacPerl.
> 
> I was expecting MacPerl (coming form Linux and Windoze)
> to brovide a rudimentary CLI to allow this, or perhaps
> (and this is what I'd like to propose):
> 
> MacPerl is smart and sees if there is a -s on the command line
> it prompts with an input bux for options.
> 
> Getopt::Std and Getopt::Long are modified to either produce the
> input field as above, or be a bit smarter and use some of that
> wacky Apple stuff to present appropriate input widgets (checkbox,
> input field) for each option.

Below is the code I use for getting options.

If you hold down any modifier key while starting the script, it will
prompt for options and stuff them in @ARGV.

Note that the 'use'age of Mac::Events in this code disables implicit
event processing, so you will have to call WaitNextEvent() periodically
or have your script hold up the computer while it's running.

Since you say you're writing a cron replacement, just replace your
sleep() with a loop calling WaitNextEvent() with a large sleep time. Or,
you can remove the modifier key test and always prompt for options.


use Mac::Events qw(@Event $CurrentEvent WaitNextEvent
                   nullEvent cmdKey shiftKey optionKey controlKey);

....

if (get_modifiers() & (cmdKey | shiftKey | optionKey | controlKey)) {

  my $line = MacPerl::Ask("Command line arguments:", "-h");
  defined $line or exit; # user cancelled

  require Text::ParseWords;
  @ARGV = Text::ParseWords::shellwords($line);
}



....


sub get_modifiers {
  # events don't seem to work for these versions of MacPerl:
  # 5.6.1aX: 1-2 crash, 3-4 don't produce null events
  return 0 if $MacPerl::Version =~ /^\Q5.6.1a[1-4]/; 
  
  # this is ugly, but Mac::Events doesn't give any way to just
  # check for the current state of modifier keys

  my ($got_it, $stop, $result) = (0, 0, 0);

  local $Event[nullEvent] = sub {
    $got_it = 1;
    $result = $CurrentEvent->modifiers;
  };

  WaitNextEvent(0) until $got_it or $stop++ > 20;
  return $result;
}

-- 
Kevin Reid

Reply via email to