Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / PHP, Perl, Python [игнор отключен] [закрыт для гостей] / [Perl] Странности с Getopt / 3 сообщений из 3, страница 1 из 1
18.05.2013, 13:08
    #38263189
Alibek B
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
[Perl] Странности с Getopt
Есть такой код:
Код: php
1.
2.
3.
4.
5.
use Getopt::Long;
my $help = '';
my $ret = GetOptions('help' => \$help);
print Dumper($ret);
print Dumper($help);


Он выдает такой результат:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
C:\Perl>iptv --help
$VAR1 = 1;
$VAR1 = '';

C:\Perl>iptv
$VAR1 = 1;
$VAR1 = '';

Не могу понять почему. Не подскажите?

Версия:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
C:\Perl>perl -v

This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x64-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2012, Larry Wall

Binary build 1601 [296175] provided by ActiveState  http://www.ActiveState.com 
Built Aug 30 2012 18:41:50

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at  http://www.perl.org/,  the Perl Home Page.
________________________
Мы смотрим с оптимизмом...
...в оптический прицел.
...
Рейтинг: 0 / 0
18.05.2013, 13:49
    #38263219
Alibek B
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
[Perl] Странности с Getopt
Похоже особенности реализации для Windows.
Пришлось такой хак делать:
Код: php
1.
@ARGV = split(' ',decode('cp1251',join(' ',@ARGV)));


(decode — чтобы из консоли кириллица правильно читалась)
...
Рейтинг: 0 / 0
28.11.2013, 04:10
    #38481015
volodin661
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
[Perl] Странности с Getopt
Alibek B.Есть такой код:
Код: php
1.
2.
3.
4.
5.
use Getopt::Long;
my $help = '';
my $ret = GetOptions('help' => \$help);
print Dumper($ret);
print Dumper($help);


Он выдает такой результат:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
C:\Perl>iptv --help
$VAR1 = 1;
$VAR1 = '';

C:\Perl>iptv
$VAR1 = 1;
$VAR1 = '';

Не могу понять почему. Не подскажите?



Потому что требуется прочитать описание того модуля, которым собираешься пользоваться, прежде чем им пользоваться.

Getopt::Long::GetOptions положит что либо в по ссылке на хэш, если ему явно указать, что это хэш.

например, так: ( скопировано из http://perldoc.org )

Storing options values in a hash

Sometimes, for example when there are a lot of options, having a separate variable for each of them can be cumbersome. GetOptions() supports, as an alternative mechanism, storing options values in a hash.

To obtain this, a reference to a hash must be passed as the first argument to GetOptions(). For each option that is specified on the command line, the option value will be stored in the hash with the option name as key.



Код: perl
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
perl -E 'use Data::Dumper; use Getopt::Long; my %h; GetOptions (\%h, "who=s","with=s@","what=s", "where=s" ); print Dumper(\%h)' - --who=графиня --with=изменившимся --with=лицом -what бежит --where пруду

------------------------------------- output --------------------------------------
$VAR1 = {
          'with' => [
                      'изменившимся',
                      'лицом'
                    ],
          'who' => 'графиня',
          'what' => 'бежит',
          'where' => 'пруду'
      };
...
Рейтинг: 0 / 0
Форумы / PHP, Perl, Python [игнор отключен] [закрыт для гостей] / [Perl] Странности с Getopt / 3 сообщений из 3, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]