Is there any way in PERL to resolve the above issue

You could always transfer the problem from runtime to compile time by using a source filter to provide an inline/macro facility. In use it would look something like this:

#! perl -slw use strict; use Macro; MACRO sub fred { my( $a, $b, $c ) = @_; return ( $a + $b ) * $c; } ENDMACRO my( $x, $y, $z ) = ( 1, 4, 7 ); print fred( $x, $y, $z ); for my $x ( 1 .. 10 ) { print fred( $x, $y, $z ); } __END__ P:\test>435608 35 35 42 49 56 63 70 77 84 91 98

And the actual code (whitespace reduced) that would be executed is:

my( $x, $y, $z ) = ( 1, 4, 7 ); print do{ our $MACRO_RETURN; local $MACRO_RETURN; $MACRO_RETURN = ( $x + $y ) * $z; goto MACRO_RETURN;; MACRO_RETURN: $MACRO_RETURN }; for my $x ( 1 .. 10 ) { print do{ our $MACRO_RETURN; local $MACRO_RETURN; $MACRO_RETURN = ( $x + $y ) * $z; goto MACRO_RETURN;; MACRO_RETURN: $MACRO_RETURN }; }

This is only a demonstration (with flaws) of the possibilities, and far from fully thought through, but it has some merits relative to the alternatives offered:

The package Macro:

package Macro; use strict; use warnings; use Filter::Simple; my %macros; FILTER_ONLY executable => sub { while( s[ MACRO\s+ sub\s+ (\w+)\s+ \{\s+ my\s*\( \s* ( [^\)]+? ) \s* \)\s* =\s* \@_;\s+ ( .*? ) \} \s* ENDMACRO ][]xsmg ) { my( $name, $args, $body ) = ( $1, $2, $3 ); $body =~ s[[\r\n]+][]g; $macros{ $name } = [ [ split ',\s*', $args ], "do{ our \$MACRO_RETURN; local \$MACRO_RETURN; $body; MACRO_RETURN: \$MACRO_RETURN }" ]; } for my $macro ( keys %macros ) { s[ $macro\( \s* ( [^\)]+ ) \s* \) ]{ my @actual = split '\s*,\s*', $1; my @formal = @{ $macros{ $macro }[ 0 ] }; my $do = $macros{ $macro }[ 1 ]; my %substs; @substs{ @formal } = @actual; $do =~ s[([\$][\w]+)]{ exists $substs{ $1 } ? $substs{ $1 } : $1 }eg; $do =~ s[return\s*(.*?)\s*;] [\$MACRO_RETURN = $1; goto MACRO_RETURN;]g; $do; }xeg; } $_; }; 1;

Source filters are either condemned as evil, hailed as saviours, or sometimes both as people transition from one state of Perlish grace to another.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re: Inline - Subroutine Overhead by BrowserUk
in thread Inline - Subroutine Overhead by santhosh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.