kobiee has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Need help to convert short c shell script to perl
by toolic (Bishop) on Sep 25, 2007 at 13:41 UTC
Re: Need help to convert short c shell script to perl
by blue_cowdawg (Monsignor) on Sep 25, 2007 at 14:17 UTC
        I need help to convert c shell script to perl.

    Dearest Monk,
    First let me start off by welcoming you to the Monastery and congratulate you on your very first post. That being said:

    Let me recommend that you visit Tutorials and in particular there are three particular pages you should read.

    Once you have a handle on how we do things here at the Monastery you could also take a look at Perl Babysteps 1: Your First Simple Script.

    Normally we like to see folks actually attempt to do their own work first and then ask questions when they have an issue getting it to work. The script you show above is a fairly simple script to convert with more than one way to go about it. Give it a shot and then check in with us if it doesn't work.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Need help to convert short c shell script to perl
by cdarke (Prior) on Sep 25, 2007 at 14:04 UTC
    I suggest you look at the file being source'd. The script you show is almost trivial in that it is just printing values. Question is, where do these values come from? probably the source'd file, but you might also have to look at .login and .cshrc.
Re: Need help to convert short c shell script to perl
by perlfan (Parson) on Sep 25, 2007 at 14:07 UTC
    source $1
    This is including a file, which is specified at the command line as your first argument. It is most likely a config file of some sort.
    echo "--0--" echo $lnPARAM[0] echo $lnVAL[0]
    This is printing the contents to two "arrays" to the screen, presumably they are defined in the file being sourced.

    So all you are doing is:
    1. specify a config file for reading
    2. read the contents, put them into arrays for later usage
    3. loop over array contents and print them out
    You generally don't "include" config files in Perl programs. Instead, you parse them out using a loop like:
    while (<STDIN>) { my $line = $_; # do something with $line }
    This handles contents piped in using a command like:
    #cat config.txt | perl myscript.pl

    More likely, though, you'll want to read a specified file using the open function.

    This is probably the most simple "real" script you could write, so it is not that hard to convert this at all.
Re: Need help to convert short c shell script to perl
by grinder (Bishop) on Sep 25, 2007 at 14:35 UTC
    I need help to convert c shell script to perl.

    Oh, quite simple. Just add this one line to the top of your file, and Presto! Instant Perl! :)

    sub source{require$_[0]}sub echo{print"$_\n"for@_}BEGIN{/($ARGV[0])/;open I,$0;<I>;eval while<I>}

    Remember, even if the above line wraps, it should be on one line in your file.

    • another intruder with the mooring in the heart of the Perl

Re: Need help to convert short c shell script to perl
by Tux (Canon) on Sep 25, 2007 at 14:10 UTC

    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
          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