The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]




Версия для распечатки Пред. тема | След. тема
Новые ответы [ Отслеживать ]
STDIN как вызов переменной., !*! Аноним, 11-Фев-11, 17:56  [смотреть все]
Добрый день.
Вот скрипт, задача - если на STDIN пришло "orange" то вывести переменную $orange те. "Апельсин" и так далее, хотелось бы обойтись без кучи if'ов.
-----------------------
#!/usr/bin/perl -w
use strict;
use utf8;

my $orange = 'Апельсин';
my $apple = 'Яблоко';

my $msg=(<STDIN>);
$msg =~ s/(\n)//g;

print "$$msg";

-----------------------
Скрипт выдает ошибку:
Can't use string ("orange") as a SCALAR ref while "strict refs" in use at ./test.pl line 11, <STDIN> line 1.

Если 'strict refs' убрать то:
Use of uninitialized value in string at ./test.pl line 11, <STDIN> line 1.
Warning: something's wrong at ./test.pl line 11, <STDIN> line 1.

Ну или как правильно добиться нужной функциональности, заранее спасибо.

  • STDIN как вызов переменной., !*! NuINu, 18:37 , 11-Фев-11 (1)
    > Добрый день.
    > Вот скрипт, задача - если на STDIN пришло "orange" то вывести переменную
    > $orange те. "Апельсин" и так далее, хотелось бы обойтись без кучи
    > if'ов.

    добиться можно, но нужно ли? нужно использовать при обьявлении не my а local.


    !/usr/bin/perl                                                                                                                                                          
    use strict;                                                                                                                                                              
    #use warnings;                                                                                                                                                          
    #use utf8;                                                                                                                                                              
                                                                                                                                                                            
                                                                                                                                                                            
    #no strict "vars";                                                                                                                                                      
    local $main::orange = 'Апельсин';                                                                                                                                        
    local $main::apple = 'Яблоко';                                                                                                                                          
                                                                                                                                                                            
                                                                                                                                                                            
    my $msg=(<STDIN>);                                                                                                                                                      
    chomp $msg;                                                                                                                                                              
                                                                                                                                                                            
    print "msg_name: '$msg'\n";                                                                                                                                              
    { no strict 'refs';                                                                                                                                                      
    print "msg: '${$msg}'\n";                                                                                                                                                
    }                                                                                                                                                                        
                              

  • STDIN как вызов переменной., !*! erera22, 18:50 , 11-Фев-11 (2)
    >[оверквотинг удален]
    > print "$$msg";
    > -----------------------
    > Скрипт выдает ошибку:
    > Can't use string ("orange") as a SCALAR ref while "strict refs" in
    > use at ./test.pl line 11, <STDIN> line 1.
    > Если 'strict refs' убрать то:
    > Use of uninitialized value in string at ./test.pl line 11, <STDIN> line
    > 1.
    > Warning: something's wrong at ./test.pl line 11, <STDIN> line 1.
    > Ну или как правильно добиться нужной функциональности, заранее спасибо.

    Как-то все это гибло выглядит, не лучше так (на Perl):
    #!/usr/bin/perl -w
    use strict;

    my ($var) = @ARGV;
    exit 0 if !defined $var;

    my %hash = (
       orange   => 'Orange',
       apple    => 'Apple',
       grape    => 'Grape',
    );

    print "$hash{ $var }\n" if exists $hash{ $var };

    на bash:

    #!/bin/bash
    var=${1}
    if [ -z ${var} ]; then
       exit
    fi

    arr=(
       orange=Orange
       grape=Grape
       apple=Apple
    )

    result=$(echo ${arr[${var}]} | sed 's/^.*\=//')
    echo ${result}




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2025 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру