FAQ
Hi gurus,
I want to read the STDIN as an input and I process the input whose
output can be single field or multiple fileds which I want to store in
variables. Based on that condition I want to perform two things, may be using
sub-routines. Inside the sub-routine, if I try to refer to the variables, which
are declared earlier (outside the sub routine), it is not working, obviously.
Is there a way to declare a global variable so that I can refer to that variable
inside a if loop or a sub-routine. I am sure there is a way.

snippet of the code

$input = <STDIN> ;
# I process the input here
# If the output from previous step is a single field, then call subroutine
"test" or call sub routine "test1".
sub test {
# I would like to refer to the $input variable and variables declared in the
main part

}

sub test1 {
}


I would appreciate any help. Thanks in advance.

Thx
Kailash

PS: I am a newbie.

Search Discussions

  • Dave hoover at Jul 9, 2001 at 3:22 pm
    Kailash wrote:
    [snip]

    Start each of your scripts with the following, it will
    save you many hours of debugging:

    #!/usr/bin/perl -w
    use strict;
    $input = <STDIN> ;
    Declaring your variables with 'my' will help you keep
    track of their scope:

    my $input = <STDIN>;
    # I process the input here
    # If the output from previous step is a single
    field, then call subroutine
    "test" or call sub routine "test1".
    When you call the subroutines, pass in the variable
    like this:

    my $test_return = test($input);

    I'll explain why I used $test_return below...
    sub test {
    # I would like to refer to the $input variable and
    variables declared in the
    main part
    Inside the subroutines grab the variable like this:

    my $input = shift;

    Now do whatever you want to the variable.
    If you want to pass something back from the
    subroutine, end your subroutine like this:

    return $input;

    You don't have to use $input, you could pass anything
    back. Now $test_return will hold whatever you
    returned!
    }
    HTH

    =====
    Dave Hoover
    "Twice blessed is help unlooked for." --Tolkien
    http://www.redsquirreldesign.com/dave

    __________________________________________________
    Do You Yahoo!?
    Get personalized email addresses from Yahoo! Mail
    http://personal.mail.yahoo.com/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 9, '01 at 1:54p
activeJul 9, '01 at 3:22p
posts2
users2
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase