Perl translation needed for simple bat file

Discussion in 'Perl Misc' started by kimberly.shaffer, Mar 10, 2006.

  1. I have a batch script I would like to convert to Perl, but I'm having a
    really hard time understanding how command line arguments work.

    For instance:

    set tempfile=IDM.sql

    echo set verify off>>%tempfile%
    echo set feedback off>>%tempfile%
    echo set termout off>>%tempfile%
    echo spool %1>>%tempfile%
    echo %2>>%tempfile%
    echo spool off>>%tempfile%
    echo quit>>%tempfile%
    sqlplus -s schema/pw@instance @%tempfile%
    rem if exist %tempfile% del %tempfile%

    This starts a file, I echo command line arguments into it, it wraps it
    up into a sql file and then I use sqlplus to execute it and return the
    results.

    %1=output file
    %2=select statement string

    I am not even sure where to begin, but thought I would put this simple
    script out here, hoping someone would translate into Perl for me, and
    then maybe I could try and figure out how it worked, since I know how
    batch files work.

    thanks so much in advance
     
    kimberly.shaffer, Mar 10, 2006
    #1
    1. Advertisements

  2. If you are new to this group then you should read the FAQ. People here
    will not write code for you .

    However if you try something and it doesn't work you can post the code
    and you will get plenty of useful help. If you do not show you have
    already tried something then you are unlikely to get the best help
    available

    I would say read the docs , but I have tried

    perldoc -q "command line"
    and
    perldoc -q "ARGV"

    and a few others , and I haven't been able to turn up anything. Perhaps
    someone else may able to point you at the right doc for this ?

    The command line arguments will be in the @ARGV array.

    You can either access these directly or use the shift operator to get
    at them (see perldoc -f shift)

    E.g If you wanted to run your program as follows

    myprog.pl output.txt "select * from systables"

    your code to extract the run time arguments would be something like
    either

    use strict;
    use warnings;
    my ($opfile, $sqlstring) = ($ARGV[0], $ARGV[1]);

    or

    use strict;
    use warnings;
    my $opfile = shift;
    my $sqlstring = shift;

    Presumably you will also want to check the number of command line
    arguments passed
    is 2 ?

    To check the number of elements in an array you take the subscript of
    the last element which you can get by using the notation

    $#array_name

    and add one since array subscripts start at 0.
    So you would have (untested)

    my $num_args = $#ARGV + 1
    if($num_args != 2)
    die "Usage $0 outputfile sqlstring"; # $0 contains the
    executable name

    If you are a real newbie I would suggest reading

    perldoc perlintro

    to give you some of the basics

    Hope this helps
     
    niall.macpherson, Mar 10, 2006
    #2
    1. Advertisements

  3. Woops - apologies for the accidental top posting.
    I did not intended to include the OPs post but it ended up included
    anyway :(
     
    niall.macpherson, Mar 10, 2006
    #3
  4. Well, it's just a simple array. The only difference to a normal array is
    that the values are pre-populated by the Perl interpreter at start time of
    the program.

    jue
     
    Jürgen Exner, Mar 10, 2006
    #4
  5. trolled:
    This is nonsense. You most certainly do not speak for the "best
    help available." Whether the "best help available" helps a poster
    is in no way related to anything you might do or say.

    cordially, as always,

    rm
     
    Ronald Matthews, Mar 10, 2006
    #5
  6. kimberly.shaffer

    Ian Wilson Guest

    Have you tried writing it in Perl?
    See http://learn.perl.org/

    It is usually best to have a go at writing a Perl program yourself
    before asking here for help.
    #!perl
    use strict;
    use warnings;

    my $tempfile = 'IDM.sql';

    my $outfile = $ARGV[0] || 'default.txt';
    my $select = $ARGV[1] || 'select * from galaxy';
    open my $fh, '>', $tempfile
    or die "cannot write to '$tempfile' because $!";

    print $fh <<ENDSQL;
    set verify off
    set feedback off
    set termout off
    spool $outputfile
    $select
    spool off
    quit
    ENDSQL

    close $fh or die "problem closing tempfile - $!";
    system ('sqlplus', '-s schema/pw@instance', "\@$tempfile") == 0
    or die "failed to execute sqlplus - $?";
    # if (-e $tempfile) {
    # unlink $tempile or die "cant unlink $tempfile because $!";
    # }
    comp.lang.perl.misc isn't a software writing service, so you've gotten
    away with it this time.

    Please read the posting guidelines at
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    Ian Wilson, Mar 10, 2006
    #6
  7. I am not claiming to be 'the best help available' - far from it , I am
    a very mediocre perl developer who is stiull learning.

    However the experts such as Sinan, Anno, Abigail , Jue and many others
    who give expert advice (which I have learnt a lot from) will tend not
    to reply unless some code has been posted so I am just trying to help
    the OP along.

    Unlike you , who has in the last few days just started trolling this
    group (along with rec.sport.baseball and other groups ) I am just
    trying to be helpful.

    As Sinan would say

    ** plonk **
     
    niall.macpherson, Mar 10, 2006
    #7
  8. wrote in @p10g2000cwp.googlegroups.com:
    Thank you for the compliment, but I think Abigail, Anno, Randal, Tad,
    Uri (who am I forgetting?) are in a league of their own.

    By the way, don't feed the troll (as I was guilty of doing ;-)

    Sinan
     
    A. Sinan Unur, Mar 10, 2006
    #8
  9. kimberly.shaffer

    Paul Lalli Guest

    The other Llama/Alpaca co-authors - Tom and brian. (Though Tom may
    only post in perl.beginners...)

    Sure would be nice to have those Posting Statistics that we had a few
    years ago, eh? :p ("Why don't you go code it, Paul?") Yeah, yeah...
    that'll be my next I'm-bored-let's-go-use-Perl pet project.

    Paul Lalli
     
    Paul Lalli, Mar 10, 2006
    #9
  10. Well, different newsgroups have their own various personalities, and
    treat newbies some with disdain and some really welcomingly. I am not
    trying to have anyone write code for me, but I wanted a rosetta stone
    sort of a leaping off point where I could look at one thing I
    understand and have it translated out. Thank you very much for your
    explanation, and believe me, it helps me understand a basic perl script
    better. I am on a lot of forums and I agree with you, people should
    try to do things on their own, but at the same time, that's why we have
    these forums here. If you posted to a Remedy forum asking for a simple
    example (which I think this was, nothing hard to write IF you know what
    to write) I have been happy to take 5 minutes and write it and explain
    it. So I wasn't asking for anything I would not be willing to do from
    my center of expertise, and just because I don't know Perl that well,
    doesn't mean I can't also relay the information I learn in the future.
    I will go to the link you suggested and I will make sure in the future
    that if I ask for some explanation I will post something I have tried
    to write. Does anyone have any suggestions for userfriendly perl
    groups/forums besides this one?

    thanks!
     
    kimberly.shaffer, Mar 10, 2006
    #10
  11. As the first to reply to your post , my apologies if I was slightly
    terse in my reply. I could have posted a full solution to your problem,
    however I have learnt from this list that if I post some code (no
    matter how badly written it is and how many errors it contains) I am
    far far more likely to get useful help than if I just ask a general
    question. I was therefore trying to encourage you as a newbie to do the
    same.

    There may well be other user friendly perl groups / forums out there
    but I think if you stick around here, read the FAQ and follow the
    posting guidelines you will probably learn more here than you would on
    any other perl newsgroup. I've been using the language for about 5
    years (although I only use it as a sideline, not as my main development
    language) and I find comp.lang.perl.misc to be by far the most useful
    resource. It is also very friendly provided you don't troll or hack
    other people off by continually ignoring the posting guidelines.

    The only other one I have looked at is www.perlmonks.org which you
    might want to try out
     
    niall.macpherson, Mar 10, 2006
    #11
  12. trolled:

    There is no need to apologize for top-posting.

    cordially, as always,

    rm
     
    Ronald Matthews, Mar 10, 2006
    #12
  13. kimberly.shaffer

    Baldoni Guest

    perl.beginners, and if you plan to use cgi, perl.beginners.cgi, may be
    considered user friendly. Relatively friendly, that is - but to what I
    decline to say...
     
    Baldoni, Mar 10, 2006
    #13
  14. wrote in @v46g2000cwv.googlegroups.com:
    I found this article interesting:

    http://www.sparknotes.com/lit/twocities/

    Sinan
     
    A. Sinan Unur, Mar 10, 2006
    #14
  15. wrote in @z34g2000cwc.googlegroups.com:
    ARGV and @ARGV are documented in perldoc perlvar.

    Sinan
     
    A. Sinan Unur, Mar 10, 2006
    #15
  16. kimberly.shaffer

    robic0 Guest

    ^^^^^^
    back to class "learnt" boy!
    I pity the www forums you must inhabit. Sometimes being helpfull
    is not showing up! In usenet newsgroups there are two extreme's,
    the first is like *YOU* an asskissing homo, the second is like
    the person who wrote that last line.

    I give you credit though, at least you didn't mention he should
    be *banned*.

    Your mind won't be released when you get on a technical ng and do hero
    worship. In forums, the bad language and really upset people that go
    off in a reply on your kind of post, usually sarcastically via
    insinuation call you an asshole (if not directly). These are the folks
    with higher IQ, then they get banned, then your back to a state of peace,
    whining that your the "hepless victim. But, that is not
    what I'm doing.

    You *CANNOT* parrot phrases of your posting hero's on such as posting
    guidelines and "best help available" if not in what(?) compliance?

    The hardcore scirmishes that go on here yeild, on the whole, a positive,
    net gain for everyone. There is *NO GODDAMED* room for your ettiquite
    rules. The OP just made a simple no-meaning, abstract comment at the end
    that you just read into and went off on this increddible bad taste hero-
    worship bonanza.....

    How insulting you are to intelligence!!
    ** plonk **
     
    robic0, Mar 11, 2006
    #16
  17. kimberly.shaffer

    Dr.Ruud Guest

    Paul Lalli schreef:
    Maybe you can use this as a start:
    http://www.xs4all.nl/~rvtol/perl/ -> xover_clpm.pl
     
    Dr.Ruud, Mar 11, 2006
    #17
  18. kimberly.shaffer

    robic0 Guest

    Well, I got here late, and it looks like the regular's are finished with
    you (and proboably you don't give a shit anyway from the looks of this post)
    so here goes...

    Number fuckin #1, get the **** OFF of a computer and do construction work!!!
    Number #2, don't ever post things like "If I don't ever lernt Perl, how
    can I convert large UNIX scripts to it, huh, huh, huh????"
    Get a fuckin life off of the goddamed machine. Go back to BF2 and "shut the
    **** up".

    Then think about finishing High School that you dropped out of..
     
    robic0, Mar 12, 2006
    #18
  19. kimberly.shaffer

    l v Guest

    What is the difference between 'learnt' and 'learned'?
    These are alternative forms of the past tense and past participle of
    the verb learn. Learnt is more common in British English, and learned
    in American English. There are a number of verbs of this type (burn,
    dream, kneel, lean, leap, spell, spill, spoil etc.). They are all
    irregular verbs, and this is a part of their irregularity.
     
    l v, Mar 12, 2006
    #19
  20. kimberly.shaffer

    axel Guest

    And of course 'learned' pronounced as two syllables denotes someone
    who has learnt a lot :)

    Axel
     
    axel, Mar 15, 2006
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.