in reply to Need help to convert short c shell script to perl

Well, you obviously will never be able to write the perl equivalent of source in (t)csh scripting, and as long as we don't know what is in that, we cannot help there.

#!/usr/bin/perl -T use strict; use warnings; use Your::Module::To::Do::Csh_Like_Syntax_Source qw( csh_source ); my $cli_arg = shift or die "usage: $0 arg"; # Here be dragons csh_source ($cli_arg); # Let us assume you've managed to make this fill the # appropriate %ENV entries for (0 .. 5) { # Assuming [0] should be 0, just as 1 .. 5 print qq{--$_---\n$ENV{"InPARAM$_"}\n$ENV{"InVAL$_"}\n}; } exit;

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Need help to convert short c shell script to perl
by blue_cowdawg (Monsignor) on Sep 25, 2007 at 15:22 UTC
        Well, you obviously will never be able to write the perl equivalent of source in (t)csh scripting

    Not quite true. Here is an experiment I did:

    Given a file to be sourced in like this:

    VAR1="FOO" VAR2="FEE" VAR3="FUM" export VAR1 export VAR2 export VAR3
    Here is how I sourced it in:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %envstuff=source_in("source.sh"); print Dumper(\%envstuff); sub source_in { my $fname=shift; my @vars=(); open RIPIT,sprintf("(. $fname ; env) | ",$fname) or die $fname . $ +!; while(my $line=<RIPIT>){ chomp $line; my ($key,$val)=split("=",$line); push @vars,$key,$val; } close RIPIT; return @vars; }
    It ain't pretty but it works. Here is a truncated version of the results:
    $VAR1 = { 'VAR1' => 'FOO', 'VAR2' => 'FEE', 'VAR3' => 'FUM',
    I'm sure there is more than one way to get this done and I'll be some of them are quite elegant, but sometimes I like to just pull a hammer out of the toolbox and whack nails that stick out.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Very unlikely to be the OP's input format. You're assuming sh syntax, whereas the OP's code is csh syntax, and hence his script would more be like

      setenv InVAR0 42 setenv InVAR1 24

      and that would be easy to parse, but what if it was

      set foo=`foo` setenv bar "$foo":$PATH":"`cat /etc/PATH`

      or even worse

      set foo=`ttytype` eval $foo

      Enjoy the puzzle

      BTW I am a tcsh user myself, but I fully agree with TomC that csh/tcsh is not something to script in. Ever!


      Enjoy, Have FUN! H.Merijn
            You're assuming sh syntax, whereas the Op's code is csh syntax,

        Actually not assuming. I just defaulted to what I run into the most myself. Hardly ever touch CSH these days. I was more interested in demonstrating a concept than anything else.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg