ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; print("Enter the number of flags which needs to be generated..\n"); my $num=<STDIN>;

this is some part of my code. If the input number is 0 or less than 0 than it should jump to some other part say line number 32 of my code. how can I achieve that? any specific function which I can use?

Replies are listed 'Best First'.
Re: how to jump to some specific line in my code ?
by GrandFather (Saint) on Sep 27, 2016 at 05:08 UTC

    In Perl, and almost all "modern" languages, that is the wrong question. Think instead of writing lumps of code to handle specific small problems. In Perl we call those lumps of code "subroutines" and write them like:

    sub printString { my ($str) = @_; print $str; }

    and execute the code in the sub by "calling" it:

    printString("Hello world\n");

    There are many ways to call a specific subroutine according to some information you have. The best choice for whatever you are doing depends on many factors, but (probably) the simplest is to use a chain of if statements:

    if ($num == 1) { sub1(); } elsif ($num == 2) { sub2(); } ...

    If you need to deal with many cases that gets hard to maintain. With Perl you can instead:

    use strict; use warnings; my $num = 1; my $mainObj = bless {}; my $handler = $mainObj->can("sub$num"); die "No handler for $num\n" if !defined $handler; $handler->(); sub sub1 { print "Handling case 1\n"; }

    which reduces the maintenance to just adding a handler for each number you want to handle, but is much harder to understand without a fairly good understanding of Perl.

    If you tell us more about your problem we can give you example code that is a better fit to your situation.

    Premature optimization is the root of all job security

      Thanks !.got your point.

Re: how to jump to some specific line in my code ?
by Laurent_R (Canon) on Sep 27, 2016 at 07:32 UTC
Re: how to jump to some specific line in my code ?
by karlgoethebier (Abbot) on Sep 27, 2016 at 07:24 UTC
    "...should jump to some other part...of my code...any specific function which I can use?"

    Yes. It is called goto.

    Just mentioning it leads to immediate excommunication.

    See Why to avoid it. From ibidem: "Whether you should or shouldn't use it is long-standing religious war."

    «The Crux of the Biscuit is the Apostrophe»

Re: how to jump to some specific line in my code ?
by haukex (Archbishop) on Sep 27, 2016 at 10:43 UTC

    Hi ankit.tayal560,

    As others have already pointed out, goto LABEL should nowadays basically be avoided at all costs. GrandFather already showed you subroutines and conditionals (if), I just wanted to add something to the general list of ways to control program flow: the loop control statements next, last and redo. The advantage of these over goto is that they operate on a block of code, which has its own scope and is visible in the structure of the code. Note that the loop control statements still allow a bit of evil: they can be used across the boundaries of subroutines, which luckily generates a warning.

    Regards,
    -- Hauke D

Re: how to jump to some specific line in my code ?
by Anonymous Monk on Sep 27, 2016 at 04:58 UTC

    If the input number is 0 or less than 0 than it should jump to some other part say line number 32 of my code. how can I achieve that? any specific function which I can use?

    See

    See http://perldoc.perl.org/perlintro.html#Writing-subroutines and Modern Perl chapter 5

    use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { my $default = 0; my $num = Prompt( "Enter num flags:", $default ); if( $num eq 'purple ){ PurpleFlags( $num ); } else { BananaFlags( $num ); } } sub PurpleFlags { my( $num ) = @_; ... } sub BananaFlags { my( $num ) = @_; ...; } sub Prompt { ExtUtils::MakeMaker::prompt( @_ ); }
Re: how to jump to some specific line in my code ?
by Anonymous Monk on Sep 27, 2016 at 04:55 UTC
    This is a parody question, right? You're making a joke about 1172668?

      ?????please comment something useful rather than this!!