in reply to calling subroutines

This is one way to call your sub 5 times and display the result:
use warnings; use strict; for ( 1 .. 5 ) { some(); } sub some { print "Enter a number: \n"; chomp( my $num1 = <STDIN> ); my $num2 = 2; my $result; $result = $num1 + $num2; print "result is: $result\n"; }

If you want it to do something different, you must be much more specific. See also perlintro

Replies are listed 'Best First'.
Re^2: calling subroutines
by bobosm (Initiate) on Oct 12, 2013 at 16:06 UTC
    Tnx for reply, and how I can generate a random number for variable num1 which is in subroutine?

      Pass a parameter through your subroutine:

      my $number = rand; some($number); sub some { my $num1 = shift; print "$num1\n"; }

      But I don't have any idea how you are supposed to be reconciling that with your existing code that takes user input. Why are you taking user input from STDIN if your goal is to generate values from within the program? And is $num2 really supposed to always be the value 2?

      This is homework, right? Even on a student's limited budget, Learning Perl, from OReilly, is a worthwhile purchase. Or if you are lucky you can find it or request it at the library. Also, read perlintro, and perlsyn. You could spend countless hours stumbling around in the dark, or spend two hours reading those two free documents, after which you will be able to complete your assignments with swift confidence.

      If the web server is down for those two final links I posted, just type "perldoc perlintro", and "perldoc perlsyn" on any system that has Perl installed. They're already on that system, no network access required.


      Dave

        I know why is not oky, Yes I have read some books and rework perl.
Re^2: calling subroutines
by bobosm (Initiate) on Oct 12, 2013 at 16:16 UTC

    I write like that, but it is error: Use unitialized num1 in additional

    for ( 1 .. 5 ) { my $num1 = int(rand(10)); some(); } sub some { print "Enter a number: \n"; #chomp (my $num1 = <STDIN>); my $num1; my $num2 = 2; my $result; $result = $num1 + $num2; print "result is: $result\n"; #return result; }

      Passing variables into a subroutine through osmosis (except for specific situations that you don't need to worry about yet) is a terrible habit to get into.

      To remedy your immediate problem, remove the line from your sub, "my $num1;", because it's creating a lexical variable inside the sub that masks the variable created in your for loop. But, while that will fix the immediate problem, it will start you down the wrong path. What you should be doing is passing parameters into your subroutine like this:

      my $value = "whatever"; some($value); sub some { my( $param ) = @_; print "$param\n"; }

      ...or use shift, or any of the other means demonstrated when you read the document "perlsyn", by typing perldoc perlsyn on your local system with Perl installed.


      Dave

        Tnx man :) Now I understand the problem.
Re^2: calling subroutines
by builat (Monk) on Oct 13, 2013 at 05:46 UTC
    And some creepy code
    #!/usr/bin/perl -w #generation of some rand use strict; my $RangeVal = undef; #Get STDIN while(1){ print 'Enter qty of random values you need -> '; $RangeVal = <STDIN>; last if $RangeVal =~ /^\d+$/; } $RangeVal = &RandGen($RangeVal); #exec of func foreach (@{$RangeVal}){if(defined $_){print $_,"\n";}} #display #function declaration# sub RandGen{ my $RangeVal = shift; my @OutVector = undef; for(0..$RangeVal-1){push @OutVector,int(rand(10*$RangeVal) +);} $RangeVal = \@OutVector; return $RangeVal; }