in reply to Perl : Convert a monolithic code to a function

sub mysub { my ( $var1, $var2 ) = @_; rest of your process } Call with: mysub($var1, $var2);

It's all in the learning perl book, it's probably easiest to start there.

EDIT: No ampersand before the subroutine's name!

Replies are listed 'Best First'.
Re^2: Perl : Convert a monolithic code to a function
by user786 (Sexton) on Jun 29, 2014 at 00:58 UTC
    This is what i tried ,when i compile the code it doesn't throw an error.But it is not working, Am i missing something here?
    sub mysub { my ($var1) = @_; $ssh->exec("cmd1"); $ssh->exec("cmd2"); open my $prep, '<', $var1; while (my $config = <$prep>) { chomp $config; my $conf = $ssh->exec("$config"); print("$conf"); } mysub("config.txt");
Re^2: Perl : Convert a monolithic code to a function
by user786 (Sexton) on Jun 28, 2014 at 23:47 UTC

    Thanks for the reply. Pardon my ignorance. 1)Does $var1, $var 2 refer to the config file ? 2)And why & before mysub &mysub($var1, $var2);