Re: Could I put commands in a variable

From: Rob Dixon (rob_at_dixon.port995.com)
Date: 02/19/04


To: beginners@perl.org
Date: Thu, 19 Feb 2004 13:36:37 -0000

Joel wrote:
>
> If I was using one specific group of commands, Could I put them inside a
> variable, then just use the variable when I needed the commands instead of
> copying and pasting them?
>
> i.e.
> print "Hello world";
> if ($i == 50) {
> goto MAIN;
> }
> elsif ($t == 100) {
> goto SECONDARY;
> }
>
> as compared to
>
> $command =
> print "Hello world";
> if ($i == 50) {
> goto MAIN;
> }
> elsif ($t == 100) {
> goto SECONDARY;
> };

Hi Joel.

Both your question and your code suggest that you're not
thinking about your programming solution properly. I
don't think I've ever seen a Perl script that used 'goto'.
Not that is the Bad Thing that many people make out, it's
just usually a non-intuitive way of expressing a solution.
Also, what you describe is a subroutine, which is simply
a named piece of code. Have you come from programming BASIC
by any chance? In its earlier versions BASIC control flow
was entirely dependent on GOTO <line> and GOSUB <line>, as
it was based on the syntax of assembler languages.

I'd expect to do away with the labels and write something
like:

  sub command {

    print "Hello world";

    if ($i == 50) {
      :
    }
    elsif ($t == 100) {
      :
    }
  }

I hope this helps. It would be useful to tell us more about
what you're trying do do.

Rob

to do.



Relevant Pages

  • Re: Problem writing code to go to a label after calling a procedure
    ... >it's 'goto' commands. ... I know a good program structure avoids many goto ... >subroutine, this is somthing I found that Visual Basic can't do. ... you rip out his code and re-write it, sometimes it is Ctl V and the ...
    (comp.lang.basic.visual.misc)
  • Re: Could I put commands in a variable
    ... BASIC is the only programming I have ever done. ... remember was PRINT, GOTO, and a variety of line numbers. ... subroutines, allthough GOTO does just fine for me. ... Could I put commands in a variable ...
    (perl.beginners)
  • Re: ASCII table on ZX
    ... shell prompt have commands like GO SPECTRUM, GO C64, GO TI99 to ... launch it in respective emulation mode. ... and GOTO 48 would be parsed correctly to GOTO line 48. ... TO seems the most sensible keyword to use... ...
    (comp.sys.sinclair)
  • Re: a goto command???
    ... >>> commands perform the function better (e.g. break, continue, ... >> another break in the next loop, then an if statement on the ... >> I treat goto like I treat eval in MATLAB. ... > int i=0; ...
    (comp.soft-sys.matlab)
  • Re: Could I put commands in a variable
    ... On Feb 19, 2004, at 1:22 PM, Joel wrote: ... > remember was PRINT, GOTO, and a variety of line numbers. ... That's why we have loops, conditionals, subroutines, modules.... ...
    (perl.beginners)