Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

calling a subroutine from another subroutine

by abcdefg (Acolyte)
on Jul 06, 2012 at 19:53 UTC ( #980363=perlquestion: print w/ replies, xml ) Need Help??
abcdefg has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks, I am stuck with a very basic thing in perl code being newbie to coding.

my $usage = " Usage: $scriptname (-type<type> -OIMID <oimid> -version <version_numbe +r> -out <filename> ) -type <type> :A|B -version <version_number> :Required.Minimum Security Versio +n Number. -OEMID <oemid> : Required. -out <filename> : Required. "; my %opts; $result = GetOptions ("type=s" => \$type, "version=i" =>\$version_number, "OIMID=s" =>\$oimid, "out=s" => \$filename); if ($type eq "A"){ &Add() ; } elsif ($t eq "B"){ &Sub(); } else { die "Unhandled option.\n"; } sub StingtoNum() { if ($oimid = "Google") { return "01"; } elsif ($oimid = "DELL") { return "02"; } else { return "00"; } } sub Add() { $oim = &StingtoNum; $zero="00" $Version_Info = $oim.$zero.$version_number; } $
Now when i run this program,inspite of whatever I give the value of oimid in commandline,it takes the value of oim in Add as "01".I expect if I give oimid as dell it should give 02 for oim. What am I doing wrong? Thanks in Advance for help.

Comment on calling a subroutine from another subroutine
Download Code
Re: calling a subroutine from another subroutine
by Anonymous Monk on Jul 06, 2012 at 19:59 UTC
Re: calling a subroutine from another subroutine
by toolic (Chancellor) on Jul 06, 2012 at 20:08 UTC
    What am I doing wrong?
    Enable warnings (Tip #1 from Basic debugging checklist). This will show that you are using the wrong equality operator. I have added code to make yours runnable (and used perltidy):
    use warnings; #use strict; use Getopt::Long; $result = GetOptions( "OIMID=s" => \$oimid, ); sub StingtoNum() { if ( $oimid eq "Google" ) { return "01"; } elsif ( $oimid eq "DELL" ) { return "02"; } else { return "00"; } } sub Add() { $oim = &StingtoNum; $version = "00"; $sum = $oim . $version; print "$sum"; } Add(); print "\n";
Re: calling a subroutine from another subroutine
by roboticus (Canon) on Jul 06, 2012 at 20:14 UTC

    abcdefg:

    The first problem is that you're using the assignment operator as a string comparison operator. So the first comparison in your first subroutine is *always* true, making the subroutine return "01". Instead of "=", use "eq", the string comparison operator....

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: calling a subroutine from another subroutine
by pemungkah (Priest) on Jul 08, 2012 at 16:33 UTC
    As a minor point: this
    my $usage = " Usage: $scriptname (-type<type> -OIMID <oimid> -version <version_numbe +r> -out <filename> ) -type <type> :A|B -version <version_number> :Required.Minimum Security Versio +n Number. -OEMID <oemid> : Required. -out <filename> : Required. ";
    could be a heredoc, which would make it easier to lay it out nicely:
    my $usage = <<USAGE; Usage: $0 -type[A|B] -OEMID id -version nnnn -out filename where version: minimum security version (integer) OEMID: one of the OEM IDs fron (source here...) out: name of any writable file USAGE
    I altered the usage to conform to the more-often used standards from man pages, but you can use whatever makes sense to your users. The point is that it was easy for me to line things up because I'm writing it down exactly the way it will appear when it's printed.

    You might also consider just printing to STDOUT instead of forcing an output file; that way your script could, for instance, pipe into grep or sort.

      Usage: $ARGV[0] -type[A|B] -OEMID id -version nnnn -out filename
      I think you meant to use $0 instead of @ARGV:
      $ARGV[0] is the first argument, not the program's command name itself. See $0 for the command name.
        You're right, and I've made the edit in the code. Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-09-22 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (204 votes), past polls