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

25941Re: [PBML] Re: Eval and memory problems

Expand Messages
  • merlyn@stonehenge.com
    Jan 8, 2008
    • 0 Attachment
      >>>>> "computerdribble" == computerdribble <computerdribble@...> writes:

      computerdribble> Nope , not in there. there is nothing in the FAQ concerning Macro
      computerdribble> expansion.

      By the way, it's on *YOUR DISK*. No need to go off to the net for the FAQ.
      Type "perldoc perlfaq". And you can search it too, which I will now do for
      you, to show you the FAQ *does* cover what you're looking for.

      I suggest you read the subject lines of "perldoc perlfaq" and "perldoc perl"
      weekly until you can recite them from memory. Perl comes with a HUGE amount
      of info, but it's pointless if you don't know what's there.

      $ perldoc -q expand

      Found in /usr/libdata/perl5/pod/perlfaq4.pod
      How do I expand function calls in a string?
      (contributed by brian d foy)

      This is documented in perlref, and although it's not the easiest thing
      to read, it does work. In each of these examples, we call the function
      inside the braces used to dereference a reference. If we have a more
      than one return value, we can construct and dereference an anonymous
      array. In this case, we call the function in list context.

      print "The time values are @{ [localtime] }.\n";

      If we want to call the function in scalar context, we have to do a bit
      more work. We can really have any code we like inside the braces, so we
      simply have to end with the scalar reference, although how you do that
      is up to you, and you can use code inside the braces.

      print "The time is ${\(scalar localtime)}.\n"

      print "The time is ${ my $x = localtime; \$x }.\n";

      If your function already returns a reference, you don't need to create
      the reference yourself.

      sub timestamp { my $t = localtime; \$t }

      print "The time is ${ timestamp() }.\n";

      The "Interpolation" module can also do a lot of magic for you. You can
      specify a variable name, in this case "E", to set up a tied hash that
      does the interpolation for you. It has several other methods to do this
      as well.

      use Interpolation E => 'eval';
      print "The time values are $E{localtime()}.\n";

      In most cases, it is probably easier to simply use string concatenation,
      which also forces scalar context.

      print "The time is " . localtime . ".\n";

      How do I expand tabs in a string?
      You can do it yourself:

      1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;

      Or you can just use the Text::Tabs module (part of the standard Perl
      distribution).

      use Text::Tabs;
      @expanded_lines = expand(@lines_with_tabs);

      How can I expand variables in text strings?
      Let's assume that you have a string that contains placeholder variables.

      $text = 'this has a $foo in it and a $bar';

      You can use a substitution with a double evaluation. The first /e turns
      $1 into $foo, and the second /e turns $foo into its value. You may want
      to wrap this in an "eval": if you try to get the value of an undeclared
      variable while running under "use strict", you get a fatal error.

      eval { $text =~ s/(\$\w+)/$1/eeg };
      die if $@;

      It's probably better in the general case to treat those variables as
      entries in some special hash. For example:

      %user_defs = (
      foo => 23,
      bar => 19,
      );
      $text =~ s/\$(\w+)/$user_defs{$1}/g;




      --
      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!
    • Show all 15 messages in this topic