English 中文(简体)
1. 扩大单一环境变量
原标题:Perl one-liner to expand Unix environment variables
  • 时间:2011-10-08 13:46:16
  •  标签:
  • perl
  • unix

我用了一种能够扩大“不ix”环境变量的手稿。 该书记上课。 文字如下:

# I know this should not be typed here. This is here just for testing.
@ENV{qw(LKUP_DIR DATA_DIR CTRL_DIR MMM)} = qw(/appl1/TSS/lkup /appl1/TSS/data /appl1/TSS/ctrl Oct);
while ( <DATA> )
{
  my $line=$_;
  chomp $line;
  $line =~ s{$(w+)}{ exists $ENV{$1} ? $ENV{$1} :  $ .$1 }ge;
  $line =~ s{${(w+)}}{ exists $ENV{$1} ? $ENV{$1} :  ${ .$1. }  }ge;
  print "$line
";
}

__DATA__
${LKUP_DIR}/lookup_file.txt
${CTRL_DIR}/ctrl_file_$MMMM.txt
$CTRL_DIR/ctrl_file_$MMM.txt

我想将这一文字改为一行,但我不相信如何处理上文提及的文字中使用的固定表述中的单项报价。 我尝试如下,但当然不是:

perl -lne  $line=$_; $line =~ s{$(w+)}{ exists $ENV{$1} ? $ENV{$1} :  $ .$1 }ge; $line =~ s{${(w+)}}{ exists $ENV{$1} ? $ENV{$1} :  ${ .$1. }  }ge; print "$line";  DATA.txt

Any suggestions? (I want to do this only in Perl.)

最佳回答

I agree with @Mat: it seems a bad idea. It will be just one line but you lose on readability. Just think that you might edit a year later and still try to understand what the one liner does.

但无论如何,在Perl,你可以使用<条码>q/STRING/code>而不是<条码>。

perl -lne  $line=$_; $line =~ s{$(w+)}{ exists $ENV{$1} ? $ENV{$1} : q/$/.$1 }ge; $line =~ s{${(w+)}}{ exists $ENV{$1} ? $ENV{$1} : q/${/.$1.q/}/ }ge; print "$line";  DATA.txt
问题回答

Here s a more concise version of your script. A notable difference is that I use // instead of exists. I think that the difference, where environment variables are concerned, would be negligible. If an environment variable exists, but is undefined, what is it? If it truly would be undefined, it would cause a warning when perl prints it.

perl -wpe  s#${?(w+)}?# $ENV{$1} // $& #ge;  DATA.txt

If you still insist on using exist, simply use:

perl -wpe  s#${?(w+)}?# exists $ENV{$1} ? $ENV{$1} : $& #ge;  DATA.txt

What I did:

  • Removed the useless transition variable $line
  • Removed -l option
  • Exchanged -n and print with -p
  • Change delimiter on the substitution to not have to escape {}
  • Combined the regexes by adding }? as an optional match
  • Used $& -- entire match -- instead of trying to puzzle the parts of the match back together




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签