in reply to printing enviroment vars from regex

RnC,
This is basic implementation that you can expand as you see fit. It is pretty fragile in the assumptions which means you will have to add your own error handling.
#!/usr/bin/perl use strict; use warnings; for my $file ( @ARGV ) { if ( ! open(INPUT, '<', $file) ) { warn "Unable to open $file - skipping!\n"; next; } while ( <INPUT> ) { print and next if ! /\$\w+/; $_ =~ s/\$(\w+)/$ENV{$1}/g; print; } }

Cheers - L~R

Replies are listed 'Best First'.
Re^2: printing enviroment vars from regex
by BUU (Prior) on Aug 01, 2004 at 19:43 UTC
     $_ =~ s/\$(\w+)/$ENV{$1}/g
    I'm pretty sure you want:  0 while $_ =~ s/\$(\w+)/$ENV{$1}/;
      BUU,
      Well saying that without an explanation doesn't help me much. Of course it is possible it should be obvious to me why - but I am dense (to be read it is the weekend). It worked fine for my sample input - what advantages does yours have over mine?

      Cheers - L~R

        His version will do nested replacements, eg if $FOO expands to the text $BAR, then that will in turn be expanded as well. I don't know if that's particularly desirable, though.

        Makeshifts last the longest.