Loading ...
Sorry, an error occurred while loading the content.

question regarding local-function

Expand Messages
  • Jeff Shu
    Hi everyone: I am doing an sample exercise which demonstrate the usage of local function. the sample trying to show the localized variable in subroutine are
    Message 1 of 3 , Mar 12, 2007
    • 0 Attachment
      Hi everyone:

      I am doing an sample exercise which demonstrate the usage of local
      function.
      the sample trying to show the localized variable in subroutine are
      not visible outside the block. I got error complains that scalar
      variable declared inside subrountine need a package.

      Please see my code and help me to make it working so the code will
      working according to it's purpose. Thanks!


      use strict;
      use warnings;

      my $first = "Per";
      my $last = "Lindberg";

      &greeting($first, $last);

      3. print "-----$fname-----\n" if defined $fname;

      sub greeting
      {
      local ($fname, $lname) = @_;
      5.print "Welcome $fname!!\n";
      }

      The output for line 3 and line 5:
      3 <no output>
      5 Welcome Per!!

      Jeff
      3-12-07
    • merlyn@stonehenge.com
      ... Jeff I am doing an sample exercise which demonstrate the usage of local Jeff function. You really shouldn t worry about local() until you absolutely
      Message 2 of 3 , Mar 12, 2007
      • 0 Attachment
        >>>>> "Jeff" == Jeff Shu <santa98bn@...> writes:

        Jeff> I am doing an sample exercise which demonstrate the usage of local
        Jeff> function.

        You really shouldn't worry about local() until you absolutely need it.

        If you're looking at a tutorial that teaches "local" instead of (or before)
        "my", or mentions "associative array", please stop looking there.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • Jenda Krynicky
        Please read this: http://perl.plover.com/FAQs/Namespaces.html and see if it helps. Let us know if something is unclear :-) Jenda ===== Jenda@Krynicky.cz ===
        Message 3 of 3 , Mar 12, 2007
        • 0 Attachment
          Please read this: http://perl.plover.com/FAQs/Namespaces.html
          and see if it helps. Let us know if something is unclear :-)

          Jenda
          ===== Jenda@... === http://Jenda.Krynicky.cz =====
          When it comes to wine, women and song, wizards are allowed
          to get drunk and croon as much as they like.
          -- Terry Pratchett in Sourcery
        Your message has been successfully submitted and would be delivered to recipients shortly.