Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Passing a scalar to a subroutine

by rspishock (Monk)
on Feb 01, 2012 at 12:45 UTC ( #951202=perlquestion: print w/ replies, xml ) Need Help??
rspishock has asked for the wisdom of the Perl Monks concerning the following question:

I come seeking the wisdom of the all knowledgeable monks.

I'm working on a script that passes user input into a subroutine and compares the value to a regex for compliance. Below is a couple snippets of code to show what I'm doing. The variable names in the hash have been changed and do not reflect what I am using in my actual script.

use strict; use warnings; use diagnostics; my ($input, $value) my %vlan = ( "vlan1" => "", "vlan2" => "", "vlan3" => "", "vlan4" => "", ); sub verification { print "Entering the subroutine\n"; #added for debugging purposes print "$value"; #added for debugging purposes if ($value =~ m{(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/ (2[0-4]?$)}x) { %vlan{$value}=$input; } else { print "\t\tInvlaid response. Please try again.\n"; #enter return } print "Exiting the subroutine.\n"; #added for debugging purposes } ---lines omitted--- print "Enter IP address in x.x.x.x/x format\n"; foreach $_(keys %vlan) { print "\t$_: "; chomp ($input = <STDIN>); $input = $value; #call subroutine to check user input verification($value) }

I'm having a problem with passing $value into the subroutine. When I test my script, I get the  Entering the subroutine message followed by an error stating that I am using an undefined value, and the the closing  Exiting the subroutine message.

Can someone please point me in the direction how I can pass this value to my subroutine for verification?

Thanks for all of your help.

Comment on Passing a scalar to a subroutine
Select or Download Code
Re: Passing a scalar to a subroutine
by moritz (Cardinal) on Feb 01, 2012 at 12:52 UTC

      Outstanding, thanks for the help, it worked. Thanks for also providing me with some material to read on subroutines, I'm sure they'll be helpful with what I'm working on.

Re: Passing a scalar to a subroutine
by Anonymous Monk on Feb 02, 2012 at 15:22 UTC
    Actually argument passing isn't the problem in your code. Since you define $value in a file scope, it's visible to your subroutine w/o being passed.

    If I may draw your attention to something else?

    chomp ($input = <STDIN>); $input = $value; # <--- are we sure about this?!? #call subroutine to check user input verification($value)

    If I may suggest $value = $input?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951202]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-07-28 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (200 votes), past polls