Reading STDIN seems to be breaking my script

Discussion in 'Perl Misc' started by Ramon F Herrera, Mar 17, 2007.

  1. Anyone familiar with the Asterisk open source PBX out there? Read
    on...

    An AGI (Asterisk Gateway Interface) Perl script is very similar to a
    CGI Perl script. My problem is introduced by the code commented out
    below. As soon as the code is executed, the rest of the script doesn't
    work anymore because the prompt is either not payed or (most likely)
    it is played nut I cannot hear it.

    -Ramon

    ---------------------
    #!/bin/perl -w

    use Asterisk::AGI;


    $|=1;
    my $AGI = new Asterisk::AGI;
    my %input = $AGI->ReadParse();
    sleep(1);

    # while(<STDIN>) {
    # chomp;
    # last unless length($_);
    # if (/^agi_(\w+)\:\s+(.*)$/) {
    # $AGI{$1} = $2;
    # }
    # }
    #
    # $CallerID = $AGI{"agi_callerid"};
    # $CallerIdName = $AGI{"agi_calleridname"};

    $Welcome = "faxback/Welcome";
    $enterFaxNumber = "faxback/EnterFaxNumber";
    $Farewell = "faxback/Farewell";

    $AGI->stream_file("$Welcome", "*");

    $FaxNumber = $AGI->get_data($enterFaxNumber, "10000", "11");
    Ramon F Herrera, Mar 17, 2007
    #1
    1. Advertising

  2. Anyone familiar with the Asterisk open source PBX out there? Read
    on...

    An AGI (Asterisk Gateway Interface) Perl script is very similar to a
    CGI Perl script. My problem is introduced by the code commented out
    below. As soon as the code is executed, the rest of the script doesn't
    work anymore because the prompt is either not played or (most likely)
    it is played but I cannot hear it.

    -Ramon

    ---------------------
    #!/bin/perl -w

    use Asterisk::AGI;

    $|=1;
    my $AGI = new Asterisk::AGI;
    my %input = $AGI->ReadParse();
    sleep(1);

    # while(<STDIN>) {
    # chomp;
    # last unless length($_);
    # if (/^agi_(\w+)\:\s+(.*)$/) {
    # $AGI{$1} = $2;
    # }
    # }
    #
    # $CallerID = $AGI{"agi_callerid"};
    # $CallerIdName = $AGI{"agi_calleridname"};

    $Welcome = "faxback/Welcome";
    $enterFaxNumber = "faxback/EnterFaxNumber";
    $Farewell = "faxback/Farewell";

    $AGI->stream_file("$Welcome", "*");

    $FaxNumber = $AGI->get_data($enterFaxNumber, "10000", "11");

    [...]
    Ramon F Herrera, Mar 17, 2007
    #2
    1. Advertising

  3. Ramon F Herrera

    -berlin.de Guest

    Ramon F Herrera <> wrote in comp.lang.perl.misc:
    >
    > Anyone familiar with the Asterisk open source PBX out there? Read
    > on...
    >
    > An AGI (Asterisk Gateway Interface) Perl script is very similar to a
    > CGI Perl script. My problem is introduced by the code commented out
    > below. As soon as the code is executed, the rest of the script doesn't
    > work anymore because the prompt is either not played or (most likely)
    > it is played but I cannot hear it.
    > -Ramon
    >
    > ---------------------
    > #!/bin/perl -w
    >
    > use Asterisk::AGI;
    >
    > $|=1;
    > my $AGI = new Asterisk::AGI;
    > my %input = $AGI->ReadParse();
    > sleep(1);
    >
    > # while(<STDIN>) {
    > # chomp;
    > # last unless length($_);
    > # if (/^agi_(\w+)\:\s+(.*)$/) {
    > # $AGI{$1} = $2;


    This line accesses the hash %main::AGI. It seems unlikely that
    you want to do this. It wouldn't run under strict.

    > # }
    > # }
    > #
    > # $CallerID = $AGI{"agi_callerid"};
    > # $CallerIdName = $AGI{"agi_calleridname"};
    >
    > $Welcome = "faxback/Welcome";
    > $enterFaxNumber = "faxback/EnterFaxNumber";
    > $Farewell = "faxback/Farewell";
    >
    > $AGI->stream_file("$Welcome", "*");
    >
    > $FaxNumber = $AGI->get_data($enterFaxNumber, "10000", "11");
    >
    > [...]


    The commented-out code would very probably not work as intended
    anyhow. Make sure it does what you want it to do. As a first
    step, add "use strict;" near the beginning of the script.

    Anno
    -berlin.de, Mar 17, 2007
    #3
  4. On Mar 17, 1:00 pm, -berlin.de wrote:
    > Ramon F Herrera <> wrote in comp.lang.perl.misc:
    >
    >
    >
    >
    >
    > > Anyone familiar with the Asterisk open source PBX out there? Read
    > > on...

    >
    > > An AGI (Asterisk Gateway Interface) Perl script is very similar to a
    > > CGI Perl script. My problem is introduced by the code commented out
    > > below. As soon as the code is executed, the rest of the script doesn't
    > > work anymore because the prompt is either not played or (most likely)
    > > it is played but I cannot hear it.
    > > -Ramon

    >
    > > ---------------------
    > > #!/bin/perl -w

    >
    > > use Asterisk::AGI;

    >
    > > $|=1;
    > > my $AGI = new Asterisk::AGI;
    > > my %input = $AGI->ReadParse();
    > > sleep(1);

    >
    > > # while(<STDIN>) {
    > > # chomp;
    > > # last unless length($_);
    > > # if (/^agi_(\w+)\:\s+(.*)$/) {
    > > # $AGI{$1} = $2;

    >
    > This line accesses the hash %main::AGI. It seems unlikely that
    > you want to do this. It wouldn't run under strict.
    >
    > > # }
    > > # }
    > > #
    > > # $CallerID = $AGI{"agi_callerid"};
    > > # $CallerIdName = $AGI{"agi_calleridname"};

    >
    > > $Welcome = "faxback/Welcome";
    > > $enterFaxNumber = "faxback/EnterFaxNumber";
    > > $Farewell = "faxback/Farewell";

    >
    > > $AGI->stream_file("$Welcome", "*");

    >
    > > $FaxNumber = $AGI->get_data($enterFaxNumber, "10000", "11");

    >
    > > [...]

    >
    > The commented-out code would very probably not work as intended
    > anyhow. Make sure it does what you want it to do. As a first
    > step, add "use strict;" near the beginning of the script.
    >
    > Anno



    Thanks for your help, Anno. I took the commented out code from an AGI
    program called agi-test.agi which works and comes with the Asterisk
    distribution. However, I did neglect to place the "use strict" line,
    and after I added it I am getting all kinds of errors like this one:

    Global symbol "$Welcome" requires explicit package name at
    faxback.agi line 24.

    I guess my problem is that when I learned Perl, there was no such
    thing as "my". What the heck is that? I have a vague recollection that
    it was added to make the language object oriented? Where can I read
    about that stuff? Is there a guide about this "my" new deal for
    programmers who already know the "old" Perl?

    TIA,

    -Ramon
    Ramon F Herrera, Mar 17, 2007
    #4
  5. On 2007-03-17 18:14, Ramon F Herrera <> wrote:
    > I guess my problem is that when I learned Perl, there was no such
    > thing as "my".


    Must have been a long time ago. if I remember correctly, "my" was
    introduced with perl 5.0 in 1994.

    > What the heck is that? I have a vague recollection that
    > it was added to make the language object oriented?


    No, it just declares a lexically scoped local variable, as most other
    programming languages have them (unlike "local" which creates a
    dynamically scoped local variable).

    > Where can I read about that stuff? Is there a guide about this "my"
    > new deal for programmers who already know the "old" Perl?


    perldoc is your friend.

    perldoc -f my

    gives you a short description and also says 'See "Private Variables via
    my()" in perlsub for details'. So

    perldoc perlsub

    is what you should read. If you ares still used to perl4, you should
    probably read all of the other core docs as well.

    hp


    --
    _ | Peter J. Holzer | Blaming Perl for the inability of programmers
    |_|_) | Sysadmin WSR | to write clearly is like blaming English for
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
    Peter J. Holzer, Mar 17, 2007
    #5
  6. Ramon F Herrera

    -berlin.de Guest

    Ramon F Herrera <> wrote in comp.lang.perl.misc:
    > On Mar 17, 1:00 pm, -berlin.de wrote:
    > > Ramon F Herrera <> wrote in comp.lang.perl.misc:
    > >
    > >
    > >
    > >
    > >
    > > > Anyone familiar with the Asterisk open source PBX out there? Read
    > > > on...


    [snip, mostly code]

    > > The commented-out code would very probably not work as intended
    > > anyhow. Make sure it does what you want it to do. As a first
    > > step, add "use strict;" near the beginning of the script.
    > >
    > > Anno

    >
    >
    > Thanks for your help, Anno. I took the commented out code from an AGI
    > program called agi-test.agi which works and comes with the Asterisk
    > distribution. However, I did neglect to place the "use strict" line,
    > and after I added it I am getting all kinds of errors like this one:
    >
    > Global symbol "$Welcome" requires explicit package name at
    > faxback.agi line 24.
    >
    > I guess my problem is that when I learned Perl, there was no such
    > thing as "my". What the heck is that? I have a vague recollection that
    > it was added to make the language object oriented? Where can I read
    > about that stuff? Is there a guide about this "my" new deal for
    > programmers who already know the "old" Perl?


    Well, the new deal is Perl 5 and happened twelve years ago.

    If you are going to pick up Perl (again), you should definitely learn
    about "my()" (lexical variables, nothing to do with OO) and more things
    that were added with Perl 5. "perldoc -f my" is the documentation of
    "my()", but there's much more.

    If the code you're dealing with is not strict-safe, I'd eye it with
    suspicion.

    If it *is* strict-safe, and you have taken a snippet of (modern) Perl
    and are trying to make it run under Perl 4 style code you wrote, forget
    it. Start with the whole thing and modify it to do what you want it
    to do, sticking to the style you find, learning about (looking up) new
    features as you go along.

    Anno
    -berlin.de, Mar 17, 2007
    #6
  7. Ramon F Herrera

    Joe Smith Guest

    Ramon F Herrera wrote:
    > On Mar 17, 1:00 pm, -berlin.de wrote:
    >
    > I guess my problem is that when I learned Perl, there was no such
    > thing as "my". What the heck is that? I have a vague recollection that
    > it was added to make the language object oriented? Where can I read
    > about that stuff?


    For starters, use the command "perldoc perl" (or "man perl").
    It has a list of topics that make up Perl's core documentation set.

    In particular, you should look at this section in "perldoc perltrap":

    Perl4 to Perl5 Traps

    Practicing Perl4 Programmers should take note of the following
    Perl4-to-Perl5 specific traps.

    They're crudely ordered according to the following list:

    Discontinuance, Deprecation, and BugFix traps
    Anything that's been fixed as a perl4 bug, removed as a perl4 fea-
    ture or deprecated as a perl4 feature with the intent to encourage
    usage of some other perl5 feature.

    Parsing Traps
    Traps that appear to stem from the new parser.

    Numerical Traps
    Traps having to do with numerical or mathematical operators.

    General data type traps
    Traps involving perl standard data types.

    Context Traps - scalar, list contexts
    Traps related to context within lists, scalar statements/declara-
    tions.

    Precedence Traps
    Traps related to the precedence of parsing, evaluation, and execu-
    tion of code.

    General Regular Expression Traps using s///, etc.
    Traps related to the use of pattern matching.

    Subroutine, Signal, Sorting Traps
    Traps related to the use of signals and signal handlers, general
    subroutines, and sorting, along with sorting subroutines.

    OS Traps
    OS-specific traps.

    DBM Traps
    Traps specific to the use of "dbmopen()", and specific dbm imple-
    mentations.

    Unclassified Traps
    Everything else.

    If you find an example of a conversion trap that is not listed here,
    please submit it to <> for inclusion. Also note that
    at least some of these can be caught with the "use warnings" pragma or
    the -w switch.
    Joe Smith, Mar 17, 2007
    #7
  8. Ramon F Herrera <> wrote:

    > I guess my problem is that when I learned Perl, there was no such
    > thing as "my".


    > Where can I read
    > about that stuff?



    "Coping with Scoping":

    http://perl.plover.com/FAQs/Namespaces.html


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 18, 2007
    #8
  9. On Mar 17, 6:23 pm, Abigail <> wrote:
    >
    > 'my' was introduced in Perl 5.000, which dates from 1994. The same
    > version introduced 'strict.pm', a module that you must use to get the
    > 'Global symbol "$Welcome" requires explicit package name' error message.



    Abigail:

    I began learning Perl around 1993. The llama book did not mention the
    'my' feature. I have been writing my-less scripts, and I didn't need
    it until now. Perl is not my primary programming language, which are C
    and Java. But I be using Perl much more often now, since I chose it
    (among a wide array of choices) for my AGI and other scripts.


    >
    > I'm a bit baffled that you claim to be ignorant of Perl5 features, yet
    > you do use them.
    >


    I just lifted some code (from a demo AGI script) which I am trying to
    integrate into my code.

    -Ramon
    Ramon F Herrera, Mar 18, 2007
    #9
  10. On Sun, 18 Mar 2007 13:06:21 -0700, Ramon F Herrera wrote:

    > I began learning Perl around 1993. The llama book did not mention the
    > 'my' feature. I have been writing my-less scripts, and I didn't need it
    > until now. Perl is not my primary programming language, which are C and
    > Java. But I be using Perl much more often now, since I chose it (among
    > a wide array of choices) for my AGI and other scripts.


    Well, get a modern Perl book! Perl changed and is now my language of
    choice for almost anything. It's not perfect, not by a long way, but it
    gets the job done. Investing in learning modern Perl is a wise choice.

    M4
    -= Who hasn't written a single line of C or C++ in the past 2
    years =-
    Martijn Lievaart, Mar 18, 2007
    #10
  11. Ramon F Herrera

    Joe Smith Guest

    Ramon F Herrera wrote:

    > I began learning Perl around 1993. The llama book did not mention the
    > 'my' feature.


    Are you talking about the pink llama book? It's really out of date and
    has been replaced by the blue llama book.
    -Joe
    Joe Smith, Mar 19, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amy
    Replies:
    0
    Views:
    494
  2. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,850
    Chatoyer
    May 17, 2013
  3. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    772
    Dan Pop
    Jun 21, 2004
  4. Ben
    Replies:
    2
    Views:
    1,329
    jacob navia
    Aug 29, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    286
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page